File Handling in C Programming

When the program is terminated, the entire data is lost in C programming. If you want to keep large volume of data, it is time consuming to enter the entire data. But, if file is created, these information can be accessed using few commands.
There are large numbers of functions to handle file I/O in C language. In this tutorial, you will learn to handle standard I/O(High level file I/O functions) in C.
High level file I/O functions can be categorized as:
1.     Text file

2.     Binary file

File Operations
1.     Creating a new file
2.     Opening an existing file
3.     Reading from and writing information to a file
4.     Closing a file

Working with file

While working with file, you need to declare a pointer of type file. This declaration is needed for communication between file and program.
FILE *ptr;

Opening a file

Opening Modes in Standard I/O
File Mode
Meaning Of Mode
During Inexistence Of File
r
Open for reading.
If the file does not exist, fopen() returns NULL.
w
Open for writing.
If  the file exists, its contents are overwritten. If the file does not exist, it will be created.
a
Open for append. i.e, Data is added to end of file.
If the file does not exists, it will be created.
r+
Open for both reading and writing.
If the file does not exist, fopen() returns NULL. 
w+
Open for both reading and writing.
If  the file exists, its contents are overwritten. If the file does not exist, it will be created.
a+
Open for both reading and appending.
If the file does not exists, it will be created.

Closing a File
The file should be closed after reading/writing of a file. Closing a file is performed using library function fclose().

fclose(ptr); //ptr is the file pointer associated with file to be closed.

The Functions fprintf() and fscanf() functions.

The functions fprintf() and fscanf() are the file version of printf() and fscanf(). The only difference while using fprintf()and fscanf() is that, the first argument is a pointer to the structure FILE.

Other functions like fgetchar(), fputc() etc. can be used in similar way.

Binary Files

Depending upon the way file is opened for processing, a file is classified into text file and binary file.
If a large amount of numerical data it to be stored, text mode will be insufficient. In such case binary file is used.
Working of binary files is similar to text files with few differences in opening modes, reading from file and writing to file.

Opening modes of binary files

Opening modes of binary files are rb, rb+, wb, wb+,ab and ab+. The only difference between opening modes of text and binary files is that, b is appended to indicate that, it is binary file.

Reading and writing of a binary file.

Functions fread() and fwrite() are used for reading from and writing to a file on the disk respectively in case of binary files.

Function fwrite() takes four arguments, address of data to be written in disk, size of data to be written in disk, number of such type of data and pointer to the file where you want to write.

fwrite(address_data,size_data,numbers_data,pointer_to_file);

Function fread() also take 4 arguments similar to fwrite() function as above.


No comments:

Post a Comment