6.18.10 clearerr Function
Resets the error indicator for the stream.
Include
<stdio.h>
Prototype
void clearerr(FILE * stream);
Argument
stream
- stream to reset error indicators
Remarks
The function clears the end-of-file and error indicators for the given stream (i.e.,
feof
and ferror
will return false after the
function clearerr
is called).
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
/* This program tries to write to a file that is */
/* readonly. This causes the error indicator to */
/* be set. The function ferror is used to check */
/* the error indicator. The function clearerr is */
/* used to reset the error indicator so the next */
/* time ferror is called it will not report an */
/* error. */
#include <stdio.h>
int main(void)
{
FILE *myfile;
if ((myfile = fopen("sampclearerr.c", "r")) == NULL)
printf("Cannot open file\n");
else
{
fprintf(myfile, "Write this line to the file.\n");
if (ferror(myfile))
printf("Error\n");
else
printf("No error\n");
clearerr(myfile);
if (ferror(myfile))
printf("Still has Error\n");
else
printf("Error indicator reset\n");
fclose(myfile);
}
}
Example Output
Error
Error indicator reset