I am using fscanf in C++ as follows:
Here is my text file:
abcd
efgh
There is no space or new line after “efgh”.
Now here is my loop:
FILE* fp;
char eof = 0;
do
{
char str[20];
fscanf(fp, "%s", str);
std::cout<<str<<std::endl;
}
while((eof = fgetc(fp)) != eof)
The output i expect is:
abcd
efgh
But the actual output i get is:
abcd
efgh
efgh
I debugged a found that after reading “efgh” the value read into eof is ‘\n’ and not EOF.
The environment is linux mint.I want to know why always the last string is read 2 times.Please advice
The last string is not being read twice. The problem is the loop’s test for continuing:
This assigns
fgetc()‘s return value toeofand checks that it is not equal toeof. Something which is hard to do logically. However, whenfgetc()is called when the file is atEOF, it returns-1. That is cast to acharfor the assignment, but the sub-expression in parenthesis retains the value-1(due to type promotion rules). Comparing-1to 255 or -127 (depending on whether char is signed or unsigned) finally terminates the loop.The third time through the loop,
fscanf()fails and does not updatestr: that’s why the same value seems to have been read twice.To fix it, the most straightforward technique would be:
However, on many operating systems,
feof()doesn’t work very well withfscanf()because the end of file indication isn’t reliably set untilfscanf()fails. A more reliable, O/S-resistent technique is to use