Bback.Jone Bback.Jone - 4 months ago 27
C Question

Why can't I access EOF using fseek()?

#include <stdio.h>

int main()
FILE * fp = fopen("Introduce.txt","rt");

int i = feof(fp);

i = feof(fp);

i = feof(fp);

return 0;

I tried to access EOF positioning 'file position indicator' at the end of the file.

But the result of this code is "000".
Why does this happen?


The feof() function doesn't report that it is at EOF until you try to read some data and there is no data to read.

You can seek beyond the current EOF of a file that's open for writing (or that's open for reading and writing).

See while (!feof(file)) is always wrong for more information on why you seldom if ever need to use feof(). In some ways, feof() is a function you should forget about — most of your programs will improve if you assume it doesn't exist.