17.11 – Binary files – Writing/Reading struct objects

by subbu on October 15, 2014

Text file is a file where the data of any type is first converted into text and then stored. Say for example the salary 7560 will be first converted into individual characters and stored in the file as ‘7’, ‘5’, ‘6’ and ‘0’.
When we read the data from the file then the text type is again converted into number type.
Storing the number as individual characters take more memory than storing the number as its binary equivalent.
It takes only two bytes to store 7560 in the main memory but takes 4 Bytes to store as individual characters.

One more disadvantage is that, it suffers from the performance in writing and reading from the file because data must be converted while writing and reading from the file.

Binary file in C language

We can save the memory and improve the performance by directly writing the binary data on to the file. The file with the binary data is called a binary file. One more advantage with binary file is that, can’t be opened with any other applications.

No additional code is required to perform write/read/append operations on the binary files. The only difference is in the flag that is used to open the file.

FILE *p;
p=fopen("filename","wb");

It opens the binary file in write mode

p=fopen("filename","rb");

It opens the binary file in read mode

p=fopen("filename","ab");

It opens the binary file in append mode

p=fpen("filename","wb+");

It opens the binary file in write/read mode

p=fopen("filename","rb+");

It opens the file in read/write mode

p=fopen("filename","ab+");

It opens the file in append/read mode

Writing struct objects on to the file

So far we have worked with unformatted and formatted text files. Now in this session, we will see how an object or a struct variable can be write and read from the file.

fwrite() function

It is a function used to write an object (structure variable) on to the file in a binary format. It writes “n” objects of “size” bytes each through the file pointer.
It accepts the address of an object, size of object, number of objects to be written and the file pointer as arguments.
It returns the number of actually written objects onto the file and returns -1 if any thing goes wrong in writing.

fwrite() function in C language

It accepts the address of struct variable as a void pointer because void pointer is a generic pointer and is capable to store the address of any object of any type. Through this pointer fwrite() reads the binary equivalent of struct variable from the memory and writs onto the file, hence fwrite() could work on binary files only.

Now let us see step by step procedure of writing struct variables on a file

FILE *p;
p=fopen("address","wb");

It creates a binary file with the name “address” in write mode

struct address
{
 char name[20],place[20];
 long int pin;
};
struct address x;
fwrite(&x,sizeof(x),1,p);

Writing on to the binary file

Here fwrite() is being used to write the object on to the file in a binary format. Here the address of object (6332), size of object (44), number of objects (1) and the file pointer are sending as arguments.

Now let us put all together as a program

#include<stdio.h>
struct address
{
 char name[20];
 char place[20];
 long int pin;
};
int main()
{
 FILE *p;
 struct address x;
 int n,i;
 p=fopen("address","wb");
 printf("How many records?");
 scanf("%d",&n);
 printf("Enter %d records:\n",n);
 for(i=1;i<=n;i++)
 {
   printf("Name:");
   scanf("%s",x.name);
   printf("Place:");
   scanf("%s",x.place);
   printf("Pin code:");
   scanf("%ld",&x.pin);
   fwrite(&x,sizeof(x),1,p);
 }
 fclose(p);
 printf("1 file created");
 return 0;
}

How many records?2
Enter 2 records:

Name:subbu
Place:Kavali
Pin code:524201

Name:Ramaiah
Place:Ulavapadu
Pin code:523201

1 file created

Reading struct objects from the file

Now we can read struct variables (objects) from the binary file “address” one by one using the function fread()

fread() function

It is the function used to read the binary data from the file and store into the object. It reads “n” items of “size” bytes each through the file pointer and assigns to the members of struct object whose address is being sent to the function.
It accepts the address of object, size of object, number object to be read and the file pointer as arguments.
Returns the number of items fetched from the file, returns -1 if any thing goes wrong in reading.

fread() function in C language

It accepts the address of struct variable as a void pointer for the same reason which we have discoursed earlier with fwrite(). Through this pointer, fread() reads the binary data from the file through the file pointer and assigns to the struct object whose address is sent.

Now we see a step by step procedure of reading binary data from the file “address”

FILE *p;
p=fopen("address","rb");

It opens the binary file “address” in read mode.

struct address
{
 char name[20],place[20];
 long int pin;
};
struct address x;
fread(&x,sizeof(x),1,p);

reading from a binary file

fread() reads the binary data from the file and write onto the object. Here the address of object (6332), size of object (44), number of objects (1) and the file pointer are sending as arguments.

Now fread() fetches one object from the file and stores into the object for each fetch. This we do until the end of file reached.

Now we will put altogether as a program

#include<stdio.h>
struct address
{
 char name[20];
 char place[20];
 long int pin;
};
int main()
{
 FILE *p;
 struct address x;
 p=fopen("address","rb");
 printf("Rcords of file:\n");
 while(1)
 {
  fread(&x,sizeof(x),1,p);
  if(feof(p)!=0)
    break;
  printf("\n\nName:%s\nPlace:%s\nPin code:%ld",x.name,x.place,x.pin);
 }
 fclose(p);
 return 0;
}

Output
Rcords of file:

Name:subbu
Place:Kavali
Pin code:524201

Name:Ramaiah
Place:Ulavapadu
Pin code:523201

Previous post:

Next post: