繁体   English   中英

MFC C ++可以访问文件时始终返回EOF

[英]MFC C++ Always returning EOF when I have access to file

我目前陷入这个问题,我没有任何解决办法。 这是关于我之前在这里问过的上一个问题。 但是,当我发现问题但不知道要解决该问题时,我将再次重申。

我的程序访问一个每24/7毫秒不断更新的文本文件。 它逐行获取数据并在每一行上进行比较。 如果有任何事情是“ amiss”(由我定义),那么我会将数据记录到.csv文件中。 该程序可以定时运行(用户定义)。

我的问题是,该程序在我的计算机上可以正常运行,但是在我的客户计算机上却没有。 我已经调试了程序,这些是我的发现。 以下是我为简化说明过程而尽可能减少的代码

int result;
char ReadLogLine[100000] = "";
FILE *readLOG_fp;
CString LogPathName;
LogPathName = Source_Folder + "\\serco.log"; //Source_Folder is found in a .ini file. Value is C:\\phython25\\xyratex\\serco_logs   
readLOG_fp = fopen(LogPathName, "r+t"); 
while ((result = fscanf(readLOG_fp, "%[^\n]\n", ReadLogLine)) != EOF) // Loops through the file till it reaches the end of file
{
    Sort_Array(); // Here is a function to sort the different lines that I grabbed from the text file
    Comp_State(); // I compare the lines here and store them into an array to be printed out
}
fclose(readLOG_fp);

GenerateCSV(); // This is my function to generate the csv and print everything out

在Sort_Array()中,我对从文本文件中抓取的行进行排序,因为它们的性质可能不同。 例如,

CStringArray LineType_A, LineType_B, LineTypeC, LineTypeD;
if (ReadLogLine == "Example line a")
{
    LineType_A.add(ReadLogLine);
}
else if (ReadLogLine == "Example line b")
{
    LineType_B.add(ReadLogLine);
}

等等。

在CompState()中,我比较每个LineType数组内的不同值,以查看是否存在任何差异。 如果不同,则将它们存储到单独的阵列中以进行打印。 一个简单的例子就是。

CStringArray PrintCSV_Array;

for (int i = 0; i <= LineType_A.GetUpperBound(); i++)
{    
    if (LineType_A.GetAt(0) == LineType_A.GetAt(1))
    {
        LineType_A.RemoveAt(0);
    }
    else 
    {
        LineType_A.RemoveAt(0);
        PrintCSV_Array.Add(LineType_A.GetAt(0);
    }
}

这样,我在数组中没有无限数量的数据。

现在转到GenerateCSV函数,它只是一个普通的函数,我在其中创建一个.csv文件并打印出PrintCSV_Array中的内容。

现在解决问题。 在我客户的计算机上,似乎没有将任何内容打印到CSV中。 我调试了程序,发现它在这里一直失败。

while ((result = fscanf(readLOG_fp, "%[^\n]\n", ReadLogLine)) != EOF) // Loops through the file till it reaches the end of file
{
    Sort_Array(); // Here is a function to sort the different lines that I grabbed from the text file
    Comp_State(); // I compare the lines here and store them into an array to be printed out
}
fclose(readLOG_fp);

它进入了while循环,因为我在实际程序中进行了一些错误检查。 进入while循环的那一刻,它就中断了,这提示我出于某种原因到达了EOF。 发生这种情况时,程序没有机会同时进入Sort_Array和Comp_State函数,因此给了我一个空白的PrintCSV_Array,什么也没打印出来。

我检查过的是

  1. 我绝对可以访问文本文件。

  2. 我的想法是因为文本文件每毫秒更新一次,它可能已被其他程序打开以写入其中,因此无法给我访问权限,或者文本文件始终处于fopen状态,因此无法为我保存任何数据读。 我对此进行了测试,当程序在我眼前看到KB的增加时,该程序就增加了价值。

  3. 我试图复制文本文件并将其粘贴到其他地方以供程序读取,这样我绝对可以完全访问它,一旦完成操作,我将其删除。 这也没给我打印。

  4. 我是否可以推断出它总是给我EOF,所以这有问题。

    while((result = fscanf(readLOG_fp,“%[^ \\ n] \\ n”,ReadLogLine))!= EOF)//循环遍历文件,直到到达文件末尾

如果是,我该如何解决? 我还能通过什么其他方式读取每一行。 我已经认真解决了所有关于此问题的想法,并需要一些帮助。

感谢大家的帮助。

错误非常明显。您可能已经看过了。。您忘了打开文件。

FILE *readLOG_fp;
CString LogPathName;
LogPathName = Source_Folder + "\\serco.log";

readLOG_fp = fopen(LogPathName.GetBuffer());
if(readLOG_fp==NULL)
  cout<<"Error: opening file\n";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM