Tests if error indicator is set.
Include
<stdio.h>
Prototype
int ferror(FILE * stream);
Argument
stream | FILE structure |
Return Value
Returns a non-zero value if error indicator is set; otherwise, returns a zero.
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 and find the error. 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