Read Integers Written to a Binary File using the function fread() */ - C File

C examples for File:File Write

Description

Read Integers Written to a Binary File using the function fread() */

Demo Code

#include <stdio.h>

int main(){//from w w w.  j a v a2 s  .  c  o m
     int i, k;
     int a[10];
     FILE *fptr;
    
     fptr = fopen("C:\\Code\\num.dat", "rb");
     if (fptr != NULL) {
         puts("File num.dat is opened successfully.");
         fread(a, sizeof(int), 10, fptr);
         for(i = 0; i < 10; i++)
            printf("%d\n", a[i]);
         k = fclose(fptr);

         if(k == -1)
            puts("File-closing failed");

         if(k == 0)
           puts("File is closed successfully.");
     }else
          puts("File-opening failed");
    
     return(0);
}

Result


Related Tutorials