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