Previous Page
Next Page

clearerr

Clears the file error and EOF flags

#include <stdio.h>
void clearerr(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.
}

See Also

feof( ), ferror( ), rewind( )


Previous Page
Next Page