gtpc2m2eC/C++ Language Support User's Guide

ferror-Test for Read/Write Errors

This function tests for read and write errors.

Format

#include <stdio.h>
int ferror(FILE *stream);

stream
The stream to be tested.

This function tests for an error when reading from, or writing to, the specified stream. If an error occurs, the error indicator for stream remains set until you close the stream, call the rewind function, or call the clearerr function.

If an incorrect parameter is detected during an input/output (I/O) function call, the error flag is not turned on.

Normal Return

The ferror function returns a nonzero value to indicate an error for the stream pointed to by stream; otherwise, it returns 0.

Error Return

Not applicable.

Programming Considerations

None.

Examples

The following example puts data out to a stream and then checks that a write error has not occurred.

#include <stdio.h>
 
int main(void)
{
   FILE *stream;
   char *string = "Important information";
   stream = fopen("myfile.dat","w");
 
   fprintf(stream, "%s\n", string);
   if (ferror(stream))
   {
      printf("write error\n");
      clearerr(stream);
   }
   if (fclose(stream))
      printf("fclose error\n");
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.