File I/O using Structures

In the beginning of this session, we looked at the way in which files with data are organized - as tables with records and fields. How does the data actually get stored in the file - here the character I/O and line I/O functions will not help, as the data has to be written in as a whole record

 A record, in C, can be defined using a structure - for example, an employee record can be defined as

struct employee
 {
    char *name;
    char *roll _no;
    int salary;
}emp1;

/* recall the structure declarations given in the previous session */

Writing records onto files

The fwrite () function allows a structure to be written as a whole into a file.

fwrite (&structure, sizeof-one-record, no-of-records, file-pointer);

The first parameter passes the address of the data to be written to the function. This can be replaced by a pointer variable initialized to the address of the structure.

The second is the size of the data to be written. The sizeof operator can be used to determine the size of any data in C, instead of manually determining the size and then using it. Using the sizeof operator makes the program portable to any machine. For example. the size of int is 2 in the system in which the program is being written, when the program gets shifted across systems the place where this appears has to be located and changed. Instead, the sizeof operator can be used.

The following statement writes the structure emp1 into the file emp.dat –

fwrite (emp1, sizeof (struct employee), 1, fp);

where, & emp1 is the address of the structure employee, sizeof determines the size of the structure. Note that the size of the structure label has been specified in the parameter list, and not the size of the actual structure. The struct keyword must be specified with sizeof. The third parameter the no. of records to be written into the file, any number of records can be written using one fwrite () statement, but the size parameter will always be the size of one structure only. Finally, fp is the file pointer.

Reading records from files

Corresponding to fwrite(), the fread( ) function enables records to be read as a whole from files. The following statement reads an employee record from the file emp.dat into the structure emp1.

fread (&emp1, sizeof (struct employee),1, fp);

Like, fwrite(), fread() can be used to read more than one record. fread () returns the number of records actually read, which, in this case. is one. The following program illustrates how fwrite() can be used to write entire records onto files -

Example 7.5

#include <stdio.h>
struct employee
{
    char *name; 
    char *roll _no; 
     int salary;
}emp1;

void main()
{
    char reply= 'y";
    FILE *fp;
    fp=fopen ("emp.dat", "a+");
    while (reply== 'y') 
     {
         /* keep writing until the user says no */
         printf ("
Enter the name of the employee"); 
         scanf ( "%s",emp1.name) ; 
         fflush (stdin);
         printf"
Enter the roll no. "); 
         scanf ("%s",emp1.roll_no); 
         fflush (stdin); 
         printf ("
Enter the salary "); 
         scanf ( "%d", &emp1.salary); 
         fflush(stdin);
     }
    rewind (fp);
    while(fread (&emp1, sizeof (struct employee), 1, fp) ==1)
    {
        if (feof ( fp) ) 
          exit();
       printf ( "

The name is :%s", emp1.name);
       printf ( "
The rollno is   :%s", emp1.rollno) ;   
       print f ("
The salary is  : %d ", emp1.salary)
     } 
     fclose (fp);
}