C File I/O Operations : fread() and fwrite()

fwrite() Function :

The fwrite() function is used to write data, records (sequence of bytes) into the file. The syntax of fread() function is as follows :
 size_t fread(void *ptr, size_t size, size_t n, FILE *fp);
Where :
  • ptr points to the address of array or structure to be written.
  • size pecifies the total number of bytes to be written into file.
  • n is the number of times the record will be written. Means the function writes n items into the file where each item occupies the number of bytes specified in the second argument (size).
  • fp is FILE pointer which points to the file where data will be written.

On success, it returns the count of the number of items successfully written to the file. On error, it returns a number less than n.

Now lets take a look at C example program.
#include <stdio.h>
#include <string.h>

struct bookDB {
  char name[20];
  char author[20];
  int price;
};

int main() {
  struct bookDB b1;
  FILE *fp;
  fp = fopen("books.dat", "wb");

  strncpy(b1.name, "C Programming", 20);
  strncpy(b1.author, "Dennis Ritchie", 20);
  b1.price = 250;
  fwrite(&b1, sizeof(b1), 1, fp);

  strncpy(b1.name, "C Sockets", 20);
  strncpy(b1.author, "John Forgers", 20);
  b1.price = 230;
  fwrite(&b1, sizeof(b1), 1, fp);

  strncpy(b1.name, "C Complete Ref.", 20);
  strncpy(b1.author, "Herbert Schildt", 20);
  b1.price = 230;
  fwrite(&b1, sizeof(b1), 1, fp);

  fclose(fp);
  printf("Data Successfully written into books.dat.\n");
  return 0;
}
Output :

Data Successfully written into books.dat.

The above code writes structure's data into file named books.dat. Also note that at line number 13, the file is opened with "wb" mode (which is write binary).

fread() Function :

The fread() function is used to read bytes from the file. The syntax of fread() function is as follows :
 size_t fread(void *ptr, size_t size, size_t n, FILE *fp);
Where :

  • ptr points to the address of the memory block where data will be stored.
  • size specifies the total number of bytes to be read from the file.
  • n is the number of times the record will be read. Means the function reads n items from the file where each item occupies the number of bytes specified in the second argument (size).
  • fp is FILE pointer which points to the file.
On success, it reads n items from the file and returns n. On error or end of the file, it returns a number less than n.

Now lets take a look at C example code.

Example 1 :
#include <stdio.h>
#include <string.h>

struct bookDB {
  char name[20];
  char author[20];
  int price;
};

int main() {
  struct bookDB bd;
  FILE *fp;
  fp = fopen("books.dat", "rb");
  if(fp == NULL) {
    printf("Error : could not nopn file.\n");
    return -1;
  }

  printf("Book Details :\n\n");
  while(fread(&bd,sizeof(bd), 1, fp) == 1) {
    printf("Name : %s\n", bd.name);
    printf("Author : %s\n", bd.author);
    printf("Price : %d\n\n", bd.price);
  }
  
  fclose(fp);
  return 0;
}
Output :

Book Details :

Name : C Programming
Author : Dennis Ritchie
Price : 250

Name : C Sockets
Author : John Forgers
Price : 230

Name : C Complete Ref.
Author : Herbert Schildt
Price : 230

Topics :