Go to the previous, next section.

End-Of-File and Errors

Many of the functions described in this chapter return the value of the macro EOF to indicate unsuccessful completion of the operation. Since EOF is used to report both end of file and random errors, it's often better to use the feof function to check explicitly for end of file and ferror to check for errors. These functions check indicators that are part of the internal state of the stream object, indicators set if the appropriate condition was detected by a previous I/O operation on that stream.

These symbols are declared in the header file `stdio.h'.

Macro: int EOF

This macro is an integer value that is returned by a number of functions to indicate an end-of-file condition, or some other error situation. With the GNU library, EOF is -1. In other libraries, its value may be some other negative number.

Function: void clearerr (FILE *stream)

This function clears the end-of-file and error indicators for the stream stream.

The file positioning functions (see section File Positioning) also clear the end-of-file indicator for the stream.

Function: int feof (FILE *stream)

The feof function returns nonzero if and only if the end-of-file indicator for the stream stream is set.

Function: int ferror (FILE *stream)

The ferror function returns nonzero if and only if the error indicator for the stream stream is set, indicating that an error has occurred on a previous operation on the stream.

In addition to setting the error indicator associated with the stream, the functions that operate on streams also set errno in the same way as the corresponding low-level functions that operate on file descriptors. For example, all of the functions that perform output to a stream--such as fputc, printf, and fflush---are implemented in terms of write, and all of the errno error conditions defined for write are meaningful for these functions. For more information about the descriptor-level I/O functions, see section Low-Level Input/Output.

Go to the previous, next section.