C Reference ยท stdio.h
C feof(), ferror() and clearerr() functions
Reference for C feof(), ferror() and clearerr(): what the end-of-file and error indicators mean, why while(!feof) is a bug, and how to find out why a read stopped.
Every stream carries two flags: an end-of-file indicator and an error indicator. feof() tells you whether the first is set, ferror() whether the second is, and clearerr() resets both. They answer one question: why did my last read or write fail?
The key rule is that feof() becomes true only after a read has tried to go past the end. It does not predict the end. That is why while (!feof(f)) as a loop condition is one of the most common bugs in C.
#include <stdio.h> int feof(FILE *stream); int ferror(FILE *stream); void clearerr(FILE *stream);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int feof(FILE *stream); int ferror(FILE *stream); void clearerr(FILE *stream);
| Name | Description |
|---|---|
stream |
Any open stream. |
Returns: feof() and ferror() return non-zero when the indicator is set and 0 otherwise (the non-zero value is not necessarily 1). clearerr() returns nothing.
Errors / undefined behavior: a NULL or closed stream is undefined behavior. These functions never change the stream except clearerr().
Usage notes
- Test the read, then ask why. Loop on the return value of the read call (
fgets() != NULL,fgetc() != EOF,fread() == count). When the loop ends,feof()andferror()say which of the two reasons applied. - Why
while (!feof(f))fails. After the last line has been read, the flag is not yet set (the reader has not tried to go past the end), so the body runs once more with a failed read. Withfgets()you reprocess the old buffer; withfscanf()orfgetc()you process garbage orEOFas if it were data. - Both flags can be set. If you only look at
feof(), a failing disk looks like a normal end. Checkferror()before you trust a short file. - Flags are sticky. Once set, they stay set until
clearerr(),rewind(), or a successfulfseek()(which clears end-of-file). Further reads on an end-of-file stream are allowed to fail immediately without reading. - Reading from a terminal. After the user types end-of-input,
feof(stdin)is true. Callingclearerr(stdin)lets the program read more, which is how simple "press Ctrl+D to finish a list, then continue" menus are written. - Checking output. After a series of
fprintf()calls, oneferror()check (or the result offclose()) is enough to know whether any of them failed. errnois separate. The flags say that something failed;errno(set by the failing call, not byferror()) says why. Read it right after the failure and useperror().scanffamily.scanf()returnsEOFfor input failure before any conversion, and a number smaller than expected for a matching failure. Only the first case impliesfeof()orferror().