C File I/O Operations : feof()

The feof() function is used to indicate the end of the file. The function returns a non-zero value if it encounters the end of the file, otherwise it returns 0. The syntax for feof() is as follows :
 int feof(FILE *fp);
Where fp is file pointer.

For example by using the below statement we can read the content of a file (binary/text) until the end of file is encountered.
while(!feof(fp)) {
  ch = getc(fp);
}
C Example Code :
#include <stdio.h>

int main() {
  char ch;
  FILE *fp;
  fp= fopen("myfile.txt", "r");
  while(!feof(fp)) {
    ch = getc(fp);
    printf("%c", ch);
  }
  printf("\n");
  fclose(fp);
  return 0;
}
Output :

Hello world, This is Sample Text.

Topics :