Read number with fread() function, check the file end of feof() function - C File

C examples for File:File Operation

Description

Read number with fread() function, check the file end of feof() function

Demo Code

#include <stdio.h>
int main() {//w w  w  . j a  v  a  2  s. c  o  m
   FILE *intFile;
   int num;

   if ((intFile = fopen("num.bin", "rb")) == NULL) {
      printf("Cannot open file\n");
      return -1;
   }

   while (fread(&num, sizeof(int), 1, intFile) == 1)
      printf("%d\n", num);

   if (feof(intFile)) 
      printf("\nEnd of list\n");
   else 
      printf("\nError reading file\n");

   fclose(intFile);
}

Related Tutorials