HP 3000 Manuals

feof [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Library Reference Manual

feof 

Tests whether the end-of-file indicator for a stream has been set.

Syntax 

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

Parameters 

stream  A pointer to a file to be tested.

Return Values 

=0      End-of-file has not been set.

!=0     End-of-file has been set.

Description 

The feof function is intended to clarify ambiguous return values from
standard I/O functions.

The feof function returns a nonzero value if the end-of-file indicator
was set on the specified stream.  It does not reset the indicator.  You
need to use the clearerr function to reset it.

Because I/O functions return EOF for end-of-file and error conditions,
you can use feof() and ferror() to distinguish between them.  Also, some
systems support I/O functions that take integer data instead of
characters.  For these functions, you need to use feof() and ferror() to
differentiate between valid data and the EOF flag.

Example 

The following program uses feof():

        #include <stdio.h>
        main(argc, argv)
        int argc;
        char *argv[ ];
        {
           int c;
           FILE *dfile, *datale, *datagt;

           if(argc != 2) {
              fprintf(stderr, "usage:  intsort filename\n");
              exit(1);
           }
           dfile = fopen(argv[1], "r");
           if(dfile == NULL) {
              fprintf("Can't open %s.\n", argv[1]);
              exit(1);
           }
           datale = fopen("dfle", "w");
           if(datale == NULL) {
              fprintf("Can't create dfle file.\n");
              exit(1);
           }
           datagt = fopen("dfgt", "w");
           if(datagt == NULL) {
              fprintf("Can't create dfgt file.\n");
              exit(1);
           }
           for(;;) {
              if((c = fgetc(dfile)) != EOF) {
                 if(c <= 'Z' && c >= 'A')
                    fputc(c, datale);
                 else
                    fputc(c, datagt);
               } else {
                    if(feof(dfile))
                       break;
                    else
                       fprintf(stderr, "error in reading file \n");
                       exit(1);
               }
           }
           exit(0);
        }

Whenever fgetc() returns an integer equal to EOF, the feof() function
checks whether the end-of-file has been reached.  If the end-of-file has
been reached, the loop and the program terminate; if not, an error
message is displayed and the program terminates.

See Also 

fopen(), ferror(), ANSI C 4.9.10.2, POSIX.1 8.1



MPE/iX 5.0 Documentation