What is File Handling?
File Handling is the process of creating, opening, reading, writing, appending, and closing files in a program. It allows data to be stored permanently, even after the program ends.
Definition
File handling in C is the process of storing and retrieving data from files using file operations.
Why Do We Need File Handling?
Without file handling:
- Data is stored only in RAM (temporary memory).
- Data is lost when the program ends.
With file handling:
- Data is stored permanently on the hard disk.
- It can be used later.
Example:
- Student records
- Employee details
- Bank transactions
- Attendance system
Steps in File Handling
- Declare a file pointer.
- Open the file.
- Perform file operations (read/write/append).
- Close the file.
File Pointer
A file pointer is used to access a file.
Syntax
FILE *fp;
FILEis a predefined data type.fpis the file pointer.
Opening a File
Use the fopen() function.
Syntax
fp = fopen("filename", "mode");
Example:
fp = fopen("student.txt", "w");
File Modes
| Mode | Meaning |
|---|---|
"r" | Read an existing file |
"w" | Write to a file (creates a new file or overwrites an existing file) |
"a" | Append data at the end of a file |
"r+" | Read and write |
"w+" | Read and write (overwrites existing content) |
"a+" | Read and append |
Writing to a File
Use fprintf().
Example
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt", "w");
fprintf(fp, "Welcome to File Handling in C.");
fclose(fp);
printf("Data written successfully.");
return 0;
}
Output
Data written successfully.
Contents of student.txt:
Welcome to File Handling in C.
Reading from a File
Use fgets().
Example
#include <stdio.h>
int main()
{
FILE *fp;
char text[100];
fp = fopen("student.txt", "r");
fgets(text, 100, fp);
printf("%s", text);
fclose(fp);
return 0;
}
Output
Welcome to File Handling in C.
Appending Data
Use append mode ("a") to add data without deleting the existing content.
Example
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt", "a");
fprintf(fp, "\nThis is appended text.");
fclose(fp);
return 0;
}
File Contents
Welcome to File Handling in C.
This is appended text.
Closing a File
Use fclose().
Syntax
fclose(fp);
Closing a file:
- Saves changes.
- Frees system resources.
Checking if the File Opened Successfully
Always check if fopen() returns NULL.
Example
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt", "r");
if (fp == NULL)
{
printf("File cannot be opened.");
return 1;
}
printf("File opened successfully.");
fclose(fp);
return 0;
}
Common File Functions
| Function | Purpose |
|---|---|
fopen() | Opens a file |
fclose() | Closes a file |
fprintf() | Writes formatted data to a file |
fscanf() | Reads formatted data from a file |
fgetc() | Reads one character |
fputc() | Writes one character |
fgets() | Reads one line |
fputs() | Writes one line |
Memory Diagram
Program
|
|
FILE Pointer (fp)
|
|
student.txt
-------------------------
Welcome to C
File Handling
-------------------------
The file pointer connects the program to the file.
Real-Life Example
Think of a Notebook.
- Notebook โ File
- Pen โ File pointer
- Writing in the notebook โ
fprintf() - Reading from the notebook โ
fgets() - Closing the notebook โ
fclose()
Structure of a File Program
FILE *fp;
fp = fopen("data.txt", "w");
fprintf(fp, "Hello");
fclose(fp);
Advantages of File Handling
- Stores data permanently.
- Handles large amounts of data.
- Enables data sharing between program executions.
- Reduces memory usage.
- Makes applications more reliable.
Exam Definition
File handling in C is the process of creating, opening, reading, writing, appending, and closing files to store data permanently using file operations.
