ferror
ferror is standard library function in stdio.h.[1] It is mainly used to check an error in function, checks error asociated with stream. This indicator is generally set by a previous operation on the stream that failed.
function prototype
ferror function is declared as follows
int ferror(FILE *stream);
stream is the parameter which is pointer to FILE object that identifies the stream.
Return Value
ferror() function returns non zero if and only if error indicator is associated with it. otherwise it will return zero value.
Example Code
/* ferror example: writing error */
#include <stdio.h>
int main ()
{
FILE * pFile;
pFile=fopen("myfile.txt","r");
if (pFile==NULL) perror ("Error opening file");
else {
fputc ('x',pFile);
if (ferror (pFile))
printf ("Error Writing to myfile.txt\n");
fclose (pFile);
}
return 0;
}
This program will open File myfile.txt in read only mode. If error in writing a program it will all call function ferror.
Output
It will produce an error in writing in a file.
References
- ^ ISO/IEC 9899:1999 specification (PDF). p. 317, § 7.19.10.3.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
CERT C++ Secure Coding Standard: ERR01-CPP. Use ferror() rather than errno to check for FILE stream errors
ISO/IEC 9899:1999 Section 6.3.1.1, "Boolean, characters, and integers," Section 7.1.4, and Section 7.9.10.3, "The ferror function"
ISO/IEC TR 24772 "NZN Returning error status"
External links
- The Single UNIX Specification, Version 4 from The Open Group – System Interfaces Reference,
- C++ reference for
std::ferror