feof()

Checks the end-of-file status of a stream.

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

stream

A pointer to a file stream.

Remarks

The feof() function checks the end-of-file status of the last read operation on stream. The function does not reset the end-of-file status.

This facility may have limited capability on configurations of the EWL that run on platforms that do not have console input/output or a file system.

feof() returns a nonzero value if the stream is at the end-of-file and returns zero if the stream is not at the end-of-file.

Listing: Example of feof() usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *f;

static char filename[80], buf[80] = "";

// get a filename from the user

printf("Enter a filename to read.\n");

gets(filename);

// open the file for input

if (( f = fopen(filename, "r")) == NULL) {

printf("Can't open %s.\n", filename);

exit(1);

}

// read text lines from the file until

// feof() indicates the end-of-file

for (; feof(f) == 0 ; fgets(buf, 80, f) )

printf(buf);

// close the file

fclose(f);

return 0;

}

Output:

Enter a filename to read. itwerks

The quick brown fox jumped over the moon.