More on Files (Part 2)


Lecture 23

Record 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.


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-

Text and Binary Files

All the programs that we wrote in this chapter so far worked on text files. Some of them would not work correctly on binary files. A text file contains only textual information like alphabets, digits and special symbols. In actuality the ASCII codes of these characters are stored in text files. A good example of a text file is any C program, say PR1.C.
As against this, a binary file is merely a collection of bytes. This collection might be a compiled version of a C program (say PR1.EXE), or music data stored in a wave file or a picture stored in a graphic file. A very easy way to find out whether a file is a text file or a binary file is to open that file in Turbo C/C++. If on opening the file you can make out what is displayed then it is a text file, otherwise it is a binary file.
As mentioned while explaining the file-copy program, the program cannot copy binary files successfully. We can improve the same program to make it capable of copying text as well as binary files as shown below.
Using this program we can comfortably copy text as well as binary files. Note that here we have opened the source and target files in “rb” and “wb” modes respectively. While opening the file in text mode we can use either “r” or “rt”, but since text mode is the default mode we usually drop the ‘t’.

The record I/O program that we did in an earlier section has two disadvantages:
  • The numbers (basic salary) would occupy more number of bytes, since the file has been opened in text mode. This is because when the file is opened in text mode, each number is stored as a character string.
§  If the number of fields in the structure increase (say, by adding address, house rent allowance etc.), writing structures using fprintf( ), or reading them using fscanf( ), becomes quite clumsy.
Let us now see a more efficient way of reading/writing records (structures). This makes use of two functions fread( ) and fwrite( ). We will write two programs, first one would write records to the file and the second would read these records from the file and display them on the screen.
Most of this program is similar to the one that we wrote earlier, which used fprintf( ) instead of fwrite( ). Note, however, that the file “EMP.DAT” has now been opened in binary mode.
The information obtained from the keyboard about the employee is placed in the structure variable e. Then, the following statement writes the structure to the file:
fwrite ( &e, sizeof ( e ), 1, fp ) ;
Here, the first argument is the address of the structure to be written to the disk.
The second argument is the size of the structure in bytes. Instead of counting the bytes occupied by the structure ourselves, we let the program do it for us by using the sizeof( ) operator. The sizeof( ) operator gives the size of the variable in bytes. This keeps the program unchanged in event of change in the elements of the structure.
The third argument is the number of such structures that we want to write at one time. In this case, we want to write only one structure at a time. Had we had an array of structures, for example, we might have wanted to write the entire array at once.
The last argument is the pointer to the file we want to write to.
Now, let us write a program to read back the records written to the disk by the previous program.
Here, the fread( ) function causes the data read from the disk to be placed in the structure variable e. The format of fread( ) is same as that of fwrite( ). The function fread( ) returns the number of records read. Ordinarily, this should correspond to the third argument, the number of records we asked for... 1 in this case. If we have reached the end of file, since fread( ) cannot read anything, it returns a 0. By testing for this situation, we know when to stop reading.
As you can now appreciate, any database management application in C must make use of fread( ) and fwrite( ) functions, since they store numbers more efficiently, and make writing/reading of structures quite easy. Note that even if the number of elements belonging to the structure increases, the format of fread( ) and fwrite( ) remains same.


1 comments:

Unknown said...

Very informative article.chemical wash

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms