Write Integers to a Binary File using the function fwrite() - C File

C examples for File:File Write

Description

Write Integers to a Binary File using the function fwrite()

Demo Code

#include <stdio.h>

int main(){/*w  w  w  .j a  va  2 s  . com*/
     int i, k, m, a[20];
     FILE *fptr;
    
     for(i = 0; i < 20; i++)
          a[i] = 30000 + i;
    
     fptr = fopen("C:\\Code\\num.dat", "wb");
     if (fptr != NULL) {
          puts("File opened successfully.");
          m = fwrite(a, sizeof(int), 10, fptr);
          if (m == 10)
              puts("Data written to the file successfully.");
          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