C examples for stdio.h:ferror
function
<cstdio> <stdio.h>
int ferror ( FILE * stream );
Checks if the error indicator, returning a value different from zero if it is.
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies the stream. |
A non-zero value is returned if the error indicator is set. Otherwise, zero is returned.
The following code shows how to open an existing file in read-only mode and tries to write a character to it.
It will generate an error that is detected by ferror.
#include <stdio.h> int main ()// www .j ava2s . c o m { FILE * pFile; pFile=fopen("main.cpp","r"); if (pFile==NULL) { perror ("Error opening file"); return -1; } fputc ('x',pFile); if (ferror (pFile)) printf ("Error Writing to main.cpp\n"); fclose (pFile); return 0; }