Jump to content

ferror

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Minakshinajardhane (talk | contribs) at 15:45, 10 September 2011. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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

  1. ^ ISO/IEC 9899:1999 specification (PDF). p. 317, § 7.19.10.3.


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"