Record I/O in Files
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.
#include "stdio.h" int main() { FILE *fp; char another = 'Y'; struct emp { char name[40]; int age; float bs; }; struct emp e; fp = fopen("targetfile.txt", "w"); if (fp == NULL) { puts("Cannot open file"); exit(0); } 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); getchar(); return 0; }
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.