File handling in C is used to store data permanently in a file and to read or write that data whenever required. C provides file-handling functions through the stdio.h library.
File handling in C is used to store data
permanently in a file and to read or write that data whenever
required. C provides file-handling functions through the
stdio.h library.
Here, fp is a file pointer that is used to refer
to a file opened by the program.
A file pointer is a pointer of type
FILE used by C's file-handling functions to keep
track of an opened file. It is declared using FILE *
and is passed to functions such as fopen() and
fclose().
fp will be used to refer to the file opened by
the program.
The fopen() function is used to open a file.
It takes the file name and the required opening mode as arguments
and returns a file pointer when the file is opened successfully.
If the file cannot be opened, it returns NULL.
The file data.txt is opened in read mode.
Here, fp refers to student.txt
opened in read mode.
A program should check the return value of fopen().
If the returned pointer is NULL, the file could not
be opened successfully.
The condition checks whether the file-opening operation failed.
The second argument of fopen() specifies how the
program wants to use the file. Different modes determine whether
the file will be read, written, appended, or opened for both
reading and writing.
The r mode opens an existing file for reading.
The file must already exist. If the file cannot be opened,
fopen() returns NULL.
The program can read the existing contents of
data.txt.
The w mode opens a file for writing. If the file already exists, its previous contents are overwritten. If the file does not exist, a new file is created.
The program can write new contents to the file. Existing contents, if any, are discarded when the file is opened this way.
The a mode opens a file for adding new content at the end. Existing contents are preserved. If the file does not exist, a new file is created.
New data can be added without removing the existing contents.
The r+ mode opens an existing file for both reading and writing. The file must already exist; it does not create a new file if the file is absent.
The program can read existing contents and also write to the file.
The w+ mode opens a file for both reading and writing. If the file exists, its previous contents are overwritten. If it does not exist, a new file is created.
The program can write new contents and read them back, but the old contents are lost when an existing file is opened in this mode.
The a+ mode opens a file for reading and for adding new contents at the end. Existing contents are preserved. If the file does not exist, a new file is created.
Existing contents can be read and new contents can be added at the end of the file.
| Mode | Purpose | If File Does Not Exist | Existing Contents |
|---|---|---|---|
| r | Read | Cannot open | Preserved |
| w | Write | Creates file | Overwritten |
| a | Append | Creates file | Preserved |
| r+ | Read + Write | Cannot open | Preserved |
| w+ | Read + Write | Creates file | Overwritten |
| a+ | Read + Append | Creates file | Preserved |
After finishing the file operation, the file should be closed using fclose(). It takes the file pointer as its argument and closes the corresponding opened file.
Here, fp is the file pointer used to refer to the
file that needs to be closed.
Write a C program to create a file, write a message into it, close the file, and display a confirmation message.
fopen().fclose().
Open student.txt in "w" mode, write a
short message, check whether the file was opened successfully,
and close it after writing.
The "w" mode opens a file for writing. If the file
does not exist, it is created. If it already exists, its previous
contents are overwritten.
Data written successfully
The program uses fopen() to create/open the file,
fprintf() to write data, and fclose()
to close the file. Always check whether fopen()
returned NULL before using the file pointer.
r → Read existing file
w → Write; old contents are overwritten
a → Add data at the end
r+ → Read + Write existing file
w+ → Read + Write; old contents are overwritten
a+ → Read + Append; existing contents are preserved
Forgetting to check fp == NULL can lead to using an
invalid file pointer.
Using "w" when you want to preserve existing
contents can accidentally overwrite the file.
Forgetting fclose() after completing the file
operation is bad file-handling practice.
| Function / Mode | Remember |
|---|---|
| FILE * | Declares a file pointer. |
| fopen() | Opens a file using a specified mode. |
| r | Read existing file. |
| w | Write; existing contents are overwritten. |
| a | Append at the end. |
| r+ | Read and write an existing file. |
| w+ | Read and write; old contents are overwritten. |
| a+ | Read and append. |
| fclose() | Closes the opened file. |
fopen()?fopen() returns NULL?"r" and "w"?"a"?"r+" and "w+".fclose()?fopen() and fclose()
with a suitable program.
r, w,
a, r+, w+ and
a+ modes.
Watch a beginner-friendly explanation of file handling and file opening modes in C.
A short handwritten-style revision sheet for file-opening modes will be provided here.
Use the mind map for FILE pointer, fopen(), file modes, NULL checking and fclose().