Friday, November 4, 2011

Record I/O in Files in C programming language

So far we have dealt with reading and writing only characters and strings. What if we want to read or write numbers from/to file? Furthermore, what if we desire to read/write a combination of characters, strings and numbers? For this first we would organize this dissimilar data together in a structure and then use fprintf( )
and fscanf( ) library functions to read/write data from/to file. Following program illustrates the use of structures for writing records of employees.
/* Writes records to a file using structure */

#include "stdio.h"
main( )
{
FILE *fp ;
char another = 'Y' ;
struct emp
{
char name[40] ;
int age ;
float bs ;
} ;
struct emp e ;
fp = fopen ( "EMPLOYEE.DAT", "w" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit( ) ;
}
while ( another == 'Y' )
{
printf ( "\nEnter name, age and basic salary: " ) ;
scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
fprintf ( fp, "%s %d %f\n", e.name, e.age, e.bs ) ;
printf ( "Add another record (Y/N) " ) ;
fflush ( stdin ) ;
another = getche( ) ;
}
fclose ( fp ) ;
}
And here is the output of the program...
Enter name, age and basic salary: Sunil 34 1250.50
Add another record (Y/N) Y
Enter name, age and basic salary: Sameer 21 1300.50
Add another record (Y/N) Y
Enter name, age and basic salary: Rahul 34 1400.55
Add another record (Y/N) N
In this program we are just reading the data into a structure variable using scanf( ), and then dumping it into a disk file using fprintf( ). The user can input as many records as he desires. The procedure ends when the user supplies ‘N’ for the question ‘Add another record (Y/N)’.
The key to this program is the function fprintf( ), which writes the values in the structure variable to the file. This function is similar to printf( ), except that a FILE pointer is included as the first argument. As in printf( ), we can format the data in a variety of ways, by using fprintf( ). In fact all the format conventions of printf( ) function work with fprintf( ) as well.
Perhaps you are wondering what for have we used the function fflush( ). The reason is to get rid of a peculiarity of scanf( ). After supplying data for one employee, we would hit the enter key. What scanf( ) does is it assigns name, age and salary to appropriate variables and keeps the enter key unread in the keyboard buffer. So when it’s time to supply Y or N for the question ‘Another employee (Y/N)’, getch( ) will read the enter key from the buffer thinking that user has entered the enter key. To avoid this problem we use the function fflush( ). It is designed to remove or ‘flush out’ any data remaining in the buffer. The argument to fflush( ) must be the buffer which we want to flush out. Here we have used ‘stdin’, which means buffer related with standard input device—keyboard.
Let us now write a program that reads the employee records created by the above program. Here is how it can be done...
/* Read records from a file using structure */

#include "stdio.h"
main( )
{
FILE *fp ;
struct emp
{
char name[40] ;
int age ;
float bs ;
} ;
struct emp e ;
fp = fopen ( "EMPLOYEE.DAT", "r" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit( ) ;
}
while ( fscanf ( fp, "%s %d %f", e.name, &e.age, &e.bs ) != EOF )
printf ( "\n%s %d %f", e.name, e.age, e.bs ) ;
fclose ( fp ) ;
}
And here is the output of the program...
Sunil 34 1250.500000
Sameer 21 1300.500000
Rahul 34 1400.500000

No comments:

Post a Comment