ferror() - I/O Function

How is ferror() used in C++?
How to check file error?


ferror() is an I/O function which is used to check if there is any file error in the given stream. If the value returned is "0" indicates of no error. A value other than "0" indicates that an file error has occurred.


int ferror ( FILE * stream );

Example :

#include <stdio.h>
int main()
FILE * stri = fopen ("getceg.txt", "r");
if( ferror(stri)==0 )
perror ("No file error");
perror( "File error encountered" );
return 0;

Result :

No file error: Error 0

In the above example the function is used to check if there is any error while accessing the file "getceg.txt", it returns the result.

C++ Tutorial

Ask Questions

Ask Question