C ferror function tells if there is an error when reading a file

Syntax

C ferror function has the following format.

int ferror(FILE *stream);

C ferror function is from header file stdio.h.

Description

C ferror function checks for a file error.

It returns

  • 0 if no error has occurred and returns
  • nonzero if an error has occurred.

Use the perror() function to find out more about the error.

Example

Check to see if there is an error when reading a file by using stdio.h.


#include <stdio.h>
#include <stdlib.h>
/*  www  . ja v a  2s  .co  m*/
int main(void){
   FILE *fp;

   if((fp=fopen("test", "rb"))==NULL) {
      printf("Cannot open file.\n");
      exit(1);
   }

   putc('C', fp);
   if(ferror(fp)) {
     printf("File Error\n");
     exit(1);
   }
  
   fclose(fp);
   return 0;
}




















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h