Consider this code:
FILE * fp = fopen( filename, "r" );
int ret = fscanf(fp, "%d, %d, %d, %d, %d\n", &a, &b, &c, &d, &e);
if (ret != 5)
{
// error and exit
}
long file_pos = ftell(fp);
printf("file position: %ld\n", file_pos);
The line of file being read is:
6, 5, 3, 2, 6\r\n
That is, the file has Windows line endings.
The file position comes out to be 20, whereas I expected it to be 15.
However, if I change the file open mode to binary ("rb"), the file position is 15, as expected.
After googling this up I could not find any clue as to why this is happening, but only found that people suggest not using fscanf() ever.
But I would like to know why the file pointer is not where it should be.
fscanf may be buffering the file – ie it reads a certain sized block and then parses it to decode the contents.