Name

clearerr

Synopsis

Clears the file error and EOF flags

#include <stdio.h>
voidclearerr(FILE *fp);

The clearerr() function is useful in handling errors in file I/O routines. It clears the end-of-file (EOF) and error flags associated with a specified FILE pointer.

Example

FILE *fp;
int c;
if ((fp = fopen("infile.dat", "r")) == NULL)
  fprintf(stderr, "Couldn't open input file.\n");
else
{
  c = fgetc(fp);   // fgetc() returns a character on success;
  if (c == EOF)    // EOF means either an error or end-of-file.
  {
    if (feof(fp))
      fprintf(stderr, "End of input file reached.\n");
    else if (ferror(fp))
      fprintf(stderr, "Error on reading from input file.\n");
    clearerr(fp);    // Same function clears both conditions.
  }
  else
  { /* ... */ }      // Process the character that we read.
}

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.