File handling in C

File handling is the process of storing the data permanently which gets generated during the execution of the program. The data once stored can be retrieved later and displayed as per user requirement, the stored data can be modified and if the data is no longer required then it( data) can be permanently deleted from the storage medium.

File: 
A file is a place on the disk where a group of related data is stored.

Types of file supported by C:

Text Files
Binary Files

Difference between text file and binary file

  • Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable.
  • A newline(\n) character is converted into the carriage return-linefeed combination before being written to the disk. In binary file, these conversions will not take place.
  • In text file, a special character, whose ASCII value is 26, is inserted after the last character in the file to mark the end of file. There is no such special character present in the binary mode files to mark the end of file.
  • In text file, the text and characters are stored one character per byte. For example, the integer value 1245 will occupy 2 bytes in memory but it will occupy 5 bytes in text file. In binary file, the integer value 1245 will occupy 2 bytes in memory as well as in file.

Operations

Opening the file
Closing the file
Data manipulation

Opening a File
File is opened using the function fopen().
While opening a file we have to specify the following.

  • Name of the file.
  • Mode in which the file is to be opened.
  • A pointer variable to receive the base addresses of the file being opened.

Syntax:
FILE *fp;
fp=fopen(“Filename”,”mode”);
(Note: FILE should be in caps)

Where:

FILE:
FILE is a special inbuilt structure that helps us to declare a pointer variable to store the base address of the file being opened.

fopen():
fopen() function helps us to open the file.

Filename:
Filename is a string of characters that make up a valid filename for the operating system. It may contain two parts , a primary name and an optional period with an extension name.
Example:
data.dat
Hello
Student
Stud.dat

Mode:
Mode helps us to specify the purpose for which the file is to be opened. The file modes can be

Sno Mode Explanation
1 “w” Open the file for writing only.
FILE *fp;
fp=fopen(“filename”,”w”);
When the file is opened in write mode it gets created. If the file of the specified name is already present it will be overwritten i.e. the existing contents will be deleted.
Returns NULL if unable to open the file.
2 “r” Open the file for reading only.
FILE *fp;
fp=fopen(“filename”,”r”);
When we want to read the contents of an existing file, we open the file in read mode. After opening the file the file pointer is placed at the beginning of the file. The file should be present otherwise an error will get generated.
Returns NULL if unable to open the file.
3 “a” Open the file in append mode. That is to add data in an existing file.
FILE *fp;
fp=fopen(“filename”,”a”);
When we want to add data into an existing file then the file is opened in append mode. The newly added data gets added after the existing Data. After opening the file the file pointer is placed at the end of the file.
Returns NULL if unable to open the file
4 “w+” Same as “w” except for both reading and writing.
5 “r+” Same as “r” except for both reading and writing.
6 “a+” Same as “a” except for both reading and writing.

For Binary Files

Sno Mode Explanation
1 “wb” opens or create a binary file in writing mode
2 “rb” opens a binary file in reading mode
3 “ab” opens a binary file in append mode
4 “wb+” opens a binary file in both reading and writing mode
5 “rb+” opens a binary file in both reading and writing mode
6 “ab+” opens a binary file in both reading and writing mode

Note: We can open any number of file at the same time based on the requirement of the program.

Closing The File
Function fclose() helps us to close the file.

A file must be closed as soon as all the operations on it have been completed. This ensures that all outstanding information associated with the file is flushed out from the buffer and all the links to the file are broken. It also prevents any accidental misuse of the file.

While closing the file we have to pass the file pointer as an argument that was used while opening the file.

The fclose() function does the followling tasks:

  • Flushes any unwritten data from memory.
  • Discards any unread buffered input.
  • Frees any automatically allocated buffer
  • Finally, close the file.

Syntax:
fclose(filepointer);

Example:
fclose(fp);