Ferror Macro :

  1. “ferror” is Macro.
  2. This tests if an error has occurred on a stream or not.
  3. Tests Give Stream for Read / Write Error.

Syntax :

int ferror(FILE *stream);
  • Return Type : Integer.
  • Parameter : File Stream

Live Example :

#include<stdio.h>
int main(void)
{
FILE *fp;
    fp = fopen("myfile.txt","w");
    if (ferror(fp)) {
           printf("Error has Occured");
           clearerr(stream);
   }
}

Return Value :

  1. If Error has Occurred : Returns [Non Zero Value]
  2. If Error has no Occurred : Returns [Zero]

Explanation of the Code :

We have opened file for writing purpose in write mode. Whenever we open file for writing then we have to check whether file is opened in writing mode or not. If not then Compiler will throw the error. ferror macro will check whether there is error while opening the stream or not.

if (ferror(fp)) {
           printf("Error has Occured");
           clearerr(stream);
   }

If we found any error then we are executing the exception or error handler code.

Summary :

TypeIt is Macro Not a function
PurposeTests if an error has occurred on a stream or not
Return ValueNon Zero Value if True
Zero Value if False