Sunday 29 September 2019

feof - How does C handle EOF?

#include 

int main()
{
FILE* f=fopen("book2.txt","r");
char a[200];
while(!feof(f))
{
fscanf(f,"%s",a);

printf("%s ",a);
printf("%d\n",ftell(f));
}
fclose(f);
return 0;
}


I have the code above. book2.txt contains "abcdef abcdef" with the cursor move to a newline(ie:abcdef abcdef\n). I get the results below.




abcdef 6
abcdef 13
abcdef 19


I expect to get



abcdef 6
abcdef 13
15



What am I doing wrong?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...