Tests if error indicator is set.
Include
<stdio.h>
Prototype
int ferror(FILE *stream);
Argument
stream |
pointer to FILE
structure |
Return Value
Returns a non-zero value if error indicator is set; otherwise, returns a zero.
Example
/* 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> /* for ferror, clearerr, */
/* printf, fprintf, */
/* fopen, fclose, */
/* FILE, NULL */
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