gtpc2m2dC/C++ Language Support User's Guide

feof-Test End-of-File Indicator

This function tests the end-of-file (EOF) indicator.

Format

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

stream
The stream to be tested.

This function indicates whether the EOF flag is set for the given stream pointed to by stream.

The EOF flag is set when a user attempts to read past the EOF flag. Therefore, a read of the last character in the file does not turn on the flag. A subsequent read attempt reaches the EOF flag.

If the file has a simultaneous writer that extends the file, the flag can be turned on by the reader before the file is extended. After the extension becomes visible to the reader, a subsequent read will get the new data and set the flag appropriately. For example, if the read does not read past the EOF flag, the flag is turned off. If a file does not have a simultaneous writer that is extending the file, it is not possible to read past the EOF flag.

A successful repositioning in a file (with fsetpos, rewind, fseek) or a call to the clearerr function resets the EOF flag.

Normal Return

The feof function returns a nonzero value if (and only if) the EOF flag is set for stream; otherwise, 0 is returned.

Error Return

Not applicable.

Programming Considerations

None.

Examples

The following example scans the input stream until it reads an EOF character.

#include <stdio.h>
#include <stdlib.h>
 
main() {
 
      FILE *stream;
      int rc;
      stream = fopen("myfile.dat","r");
 
/* MYFILE.DAT contains 3 characters "abc" */
      while (1) {
         rc = fgetc(stream);
         if (rc == EOF) {
            if (feof(stream)) {
               printf("at EOF\n");
               break;
            }
            else {
               printf("error\n");
               break;
            }
         }
         else
            printf("read %c\n",rc);
      }
}

Output

read a
read b
read c
at EOF

Related Information

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