File Handling in C
What is file handling?
Normally, everything a C program stores in variables vanishes the moment the program ends. File handling lets a program save data to a file on disk so it survives — you can write it now and read it back tomorrow. This is how programs remember scores, settings, records and reports.
C does all of this through a special FILE pointer and a set of functions in <stdio.h>. The pattern is always the same: open a file, use it, close it.
The FILE pointer and fopen
To work with a file, you first open it with fopen, which gives you back a FILE pointer. Every later operation uses that pointer.
FILE *fp; // a file pointer
fp = fopen("data.txt", "w"); // open for writing
if (fp == NULL) { // opening can fail!
printf("Could not open file.\n");
return 1;
}The fopen call takes two things: the file name and a mode that says what you want to do with it.
File opening modes
| Mode | Meaning |
|---|---|
"r" | Read only — the file must already exist |
"w" | Write — creates a new file or erases an existing one |
"a" | Append — adds to the end, keeping old content |
"r+" | Read and write an existing file |
"w+" | Read and write; erases existing content |
Opening an existing file in "w" mode erases everything in it immediately. Use "a" when you want to keep the old data.
Writing to a file
Once open in a write mode, fprintf works just like printf but sends text into the file.
#include <stdio.h>
int main() {
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) return 1;
fprintf(fp, "Hello, file!\n");
fprintf(fp, "Marks: %d\n", 95);
fclose(fp); // save and close
printf("Data written.\n");
return 0;
}Data written.
Inside data.txt:
Hello, file!
Marks: 95
Reading from a file
To read, open the file in "r" mode and use fgets (for lines) or fscanf (for formatted values).
#include <stdio.h>
int main() {
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("File not found.\n");
return 1;
}
char line[100];
while (fgets(line, sizeof(line), fp) != NULL)
printf("%s", line); // print each line
fclose(fp);
return 0;
}Hello, file!
Marks: 95
The fgets loop keeps reading until it returns NULL, which marks the end of the file.
Always check and close
Two habits make file handling safe and reliable:
- Check for NULL right after
fopen— the file may be missing or locked. - Call fclose when done — it flushes buffered data to disk and frees the file.
Text you write is often held in a buffer first. fclose (or fflush) is what actually pushes it into the file. Skip it and you can lose the last part of your data.
Common mistakes
- Using the
FILEpointer without checking it for NULL first. - Opening an existing file in
"w"and accidentally erasing it. - Forgetting
fclose, which can lose buffered data. - Trying to read a file opened in write mode, or write to one opened as read-only.
Write a program that appends a new line to log.txt each time it runs (mode "a"), then a second program that reads and prints the whole log. Run the first one three times and confirm the log grows.
Summary
- File handling saves data permanently using a
FILEpointer andstdio.hfunctions. - Open with
fopen(name, mode); always check the result forNULL. - Modes:
"r"read,"w"overwrite,"a"append, and the+variants. - Write with
fprintf; read withfgetsorfscanf. - Always
fcloseto flush data and release the file.
Frequently Asked Questions
What is file handling in C?
FILE pointer and functions from stdio.h such as fopen, fprintf, fscanf and fclose.What is the FILE pointer in C?
FILE pointer is a special pointer that represents an open file. When you call fopen, it returns a FILE * that you then pass to every read, write and close function so C knows which file you mean. If fopen fails, it returns NULL.What is the difference between "w" and "a" mode in fopen?
"w" opens a file for writing and erases any existing contents, so you start with an empty file. Mode "a" (append) also writes, but it keeps the existing contents and adds new data at the end. Use "w" to overwrite and "a" to add without losing old data.Why should I always check if fopen returned NULL?
fopen returns NULL and you use that pointer anyway, the program crashes. Checking for NULL right after opening lets you handle the error safely.What happens if I forget to call fclose?
fclose flushes buffered data to disk and releases the file, so forgetting it can lose data or leak resources.