Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <stdio.h>

int  feof(FILE *stream);
int  ferror(FILE *stream);
void clearerr(FILE *stream);

Try it

C The while(!feof) bug, and the fix

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.

C clearerr and a file that keeps growing

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

C
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() and ferror() 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. With fgets() you reprocess the old buffer; with fscanf() or fgetc() you process garbage or EOF as if it were data.
  • Both flags can be set. If you only look at feof(), a failing disk looks like a normal end. Check ferror() before you trust a short file.
  • Flags are sticky. Once set, they stay set until clearerr(), rewind(), or a successful fseek() (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. Calling clearerr(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, one ferror() check (or the result of fclose()) is enough to know whether any of them failed.
  • errno is separate. The flags say that something failed; errno (set by the failing call, not by ferror()) says why. Read it right after the failure and use perror().
  • scanf family. scanf() returns EOF for input failure before any conversion, and a number smaller than expected for a matching failure. Only the first case implies feof() or ferror().