Tuesday 30 January 2018

File Handling in C

File Handling in C

             Files are a means to store data in a storage device. C file handling provides a mechanism to store output of a program in a file and read from a file on the disk. So far, we have been using printf to write output of a program on to the screen and scanf to read input from keyboard. Now we will be looking at functions which allows us to create, open and write to a text or binary files in order to permanently store the output of a program to a file or reading input from a file.

Opening a File
           
            In order to open a file, we need to use fopen( ) function. If the file is opened successfully, we assign a file pointer to the file to perform reading and writing operations. A file can be opened in different modes depending on requirement. Function to open a file i.e fopen( ) takes two arguments : char *filename and const char *mode. C supports the following file open modes :



If a file is opened in any of the write modes and the file doesn't exist, then a new file is created. If the file is opened in read mode and the file is not found, then NULL is returned by the fopen( ) function. If the "file open" operation fails for any other reason, then also NULL is returned. For handling binary files, we need to append character ' b ' to the file open modes. For e.g, rb , wb , r+b , etc.

File Read / Write Operations
             
        Open a file for writing :

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------


             In the first program, a file " file.txt " is created and some data is written into it. The file is created in the same directory in which the program file is saved.
In the second program, we read the file line by line using and then print each line on the console. We can also read a file character-by-character instead of line-by-line and write a stream to file one chracter at a time. 

We illustrate this using the next program which copies one file into another. 

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

Read / Write data to a file using structures

              Suppose we want to store employee informations in a file. The information of each employee is a record which consists of name, grade and salary. Each record is structure. We can use fprintf and fscanf to write employee records and read them back.

Following program illustrates writing and reading of structures to and from a file. 

----------------------------------------------------------------------------------------------------------------------------


 

----------------------------------------------------------------------------------------------------------------------------

            We have used fflush function in the above program because after entering a employee record, we hit enter key. So an extra ' \n ' is stored in the input buffer which is not ignored by scanf function. Now when we are suppose to enter our choice ( y/Y ), scanf will read the ' \n ' character and the loop will terminate without letting user to add another employee's record.

            If we need to store records in the binary format, then we must use fwrite function to write the record to a file and fread to read information back from the file. Following program illustrates the use of fwrite and fread functions. 

----------------------------------------------------------------------------------------------------------------------------




----------------------------------------------------------------------------------------------------------------------------


            Please note that fwrite and fread are used to write and read memory blocks. We have demonstrated using structures. However, they can be used to write and read arrays also.

Manipulation of file pointer

             In order to read from or write to any position in a file, we need to set the file pointer to point to that particular position. the fseek( ) function lets us move the file pointer to the desired location in the file. We can know the current position of the pointer using ftell( ) function.
The function fseek( fp, n, ref_pos ) takes three arguments :
fp is the file pointer, n denotes the number of bytes to move and ref_pos denotes the reference position relative to which the pointer moves. ref_pos can take one of the three constants : SEEK_SET moves the file pointer n bytes from the beginning of the file, SEEK_END moves the file pointer n bytes from the end of the file and SEEK_CUR moves the file pointer n bytes from the current position.

            There is one more function called rewind( ) which places the file pointer to the beginning of the file irrespective of its current position.

 Following program illustrates random access of file using file pointer manipulation :

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment