简体   繁体   中英

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

I am currently stuck in this problem that I do not have any idea to fix. It is regarding a previous question that I have asked here before. But I will reiterate again as I found out the problem but have no idea to fix it.

My program accesses a text file that is updated constantly every millisecond 24/7. It grabs the data line by line and does comparison on each of the line. If any thing is "amiss"(defined by me), then I log that data into a .csv file. This program can be run at timed intervals(user defined).

My problem is that this program works perfectly fine on my computer but yet it doesnt on my clients computer. I have debug the program and these are my findings. Below is my code that I have reduced as much possible to ease the explanation process

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

In Sort_Array(), I sort the lines that I grab from the text file as they could be of different nature. For example,

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);
}

and so on.

In CompState(), I compare the different values within each LineType array to see if there are any difference. If it is different, then I store them into a seperate array to print. A simple example would be.

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);
    }
}

This way I dont have an infinite amount of data in the array.

Now to the GenerateCSV function, it is just a normal function where I create a .csv file and print whatever I have in the PrintCSV_Array.

Now to the problem. In my client's computer, it seems to not print anything out to the CSV. I debugged the program and found out that it keeps failing here.

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);

It goes into the while loop fine as I did some error checking there in the actual program. The moment it goes into the while loop it breaks out of it suggesting to me it reach EOF for some reason. When that happens, the program has no chance to go into both the Sort_Array and Comp_State functions thus giving me a blank PrintCSV_Array and nothing to print out.

Things that I have checked is that

  1. I definitely have access to the text file.

  2. My thoughts were because the text file is updated every millisecond, it may have been opened by the other program to write into it and thus not giving me access OR the text file is always in an fopen state therefore not saving any data in for me to read. I tested this out and the program has value added in as I see the KB's adding up in front of my eyes.

  3. I tried to copy the text file and paste it somewhere else for my program to read, this way I definitely have full access to it and once I am done with it, Ill delete it. This gave me nothing to print aswell.

  4. Am I right to deduce that it is always giving me EOF thus this is having problems.

    while ((result = fscanf(readLOG_fp, "%[^\\n]\\n", ReadLogLine)) != EOF) // Loops through the file till it reaches the end of file

If yes, How do I fix this?? What other ways can I make it read every line. I have seriously exhausted all my ideas on this problem and need some help in this.

Thanks everyone for your help.

Error is very obvious ... you might have over looked it.. You forgot to open the file.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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