Syntax
#include <stdio.h> int ferror(FILE *stream);Description
ferror tests for an error in reading from or writing to the given stream. If an error occurs, the error indicator for the stream remains set until you close stream, call rewind, or call clearerr.
The ferror function returns a nonzero value to indicate an error on the given stream. A return value of 0 means no error has occurred.
This example puts data out to a stream and then checks that a write error has not occurred.
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *stream; char *string = "Important information"; stream = fopen("ferror.dat", "w"); fprintf(stream, "%s\n", string); if (ferror(stream)) { printf("write error\n"); clearerr(stream); return EXIT_FAILURE; } else printf("Data written to a file successfully.\n"); if (fclose(stream)) perror("fclose error"); return 0; /**************************************************************************** The output should be: Data written to a file successfully. ****************************************************************************/ }Related Information