What's the best way to check if I've reached the end of a FILE*
stream without manually keeping track of the current cursor position and file size? The implementation should work in read and read/write mode of fopen
.
I came up with this:
int iseof(FILE *fp)
{
int ch = fgetc(fp);
if(ch == EOF) return 1;
ungetc(ch, fp);
return 0;
}
This does the trick but of course it has the overhead of reading a character and potentially pushing it back into the stream. That's why I was wondering whether there was a nicer solution. Just using feof()
won't work because this will only return true
if reading at or past the end of file has occurred.
Any ideas?