December 2005
Beginner to intermediate
618 pages
20h 19m
English
clearerr
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.
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.
}Read now
Unlock full access