π File Handling in C
πΉ What is File Handling?File handling in C allows a program to store data permanently on disk, read it, and modify it later.
- Files are stored in secondary memory (hard disk)
- Variables are stored in RAM (temporary)
- Data remains available even after program ends
β Why Use File Handling?
- To save data permanently
- To store large data for future use
- Used in databases, logs, reports
- Helpful in text and data processing
π File Types in C
| File Type | Description | Example |
|---|---|---|
| Text File | Human-readable text | .txt |
| Binary File | Machine-readable data | .dat, .bin |
πͺ Steps to Handle Files in C
- Declare a file pointer
- Open a file using fopen()
- Perform read/write operations
- Close the file using fclose()
FILE *fp;
1οΈβ£ Opening a File
FILE *fopen("filename", "mode");
π File Modes in C
| Mode | Description |
|---|---|
| "r" | Read (file must exist) |
| "w" | Write (overwrite/create) |
| "a" | Append (write at end) |
| "r+" | Read + Write |
| "w+" | Write + Read |
| "a+" | Append + Read |
2οΈβ£ Writing to a File (fprintf)
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "w");
if(fp == NULL) {
printf("Error opening file");
return 1;
}
fprintf(fp, "Hello World\\n");
fprintf(fp, "Welcome to C Programming\\n");
fclose(fp);
return 0;
}
This creates data.txt and writes data into it.
3οΈβ£ Reading from a File (fgets)
#include <stdio.h>
int main() {
FILE *fp;
char str[100];
fp = fopen("data.txt", "r");
if(fp == NULL) {
printf("File not found");
return 1;
}
while(fgets(str, 100, fp) != NULL) {
printf("%s", str);
}
fclose(fp);
return 0;
}
β Use fgets() for line-by-line β Use fgetc() for character-by-character
4οΈβ£ Appending Data to a File
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "a");
fprintf(fp, "This line is appended.\\n");
fclose(fp);
return 0;
}
5οΈβ£ File Positioning Functions
| Function | Description |
|---|---|
| ftell(fp) | Returns current position |
| fseek(fp, offset, whence) | Moves file pointer |
| rewind(fp) | Moves to beginning |
6οΈβ£ Binary File Handling
β Writing to Binary File
#include <stdio.h>
struct Student {
int roll;
char name[20];
};
int main() {
FILE *fp;
struct Student s = {1, "Rahul"};
fp = fopen("student.dat", "wb");
fwrite(&s, sizeof(s), 1, fp);
fclose(fp);
return 0;
}
π Reading from Binary File
#include <stdio.h>
struct Student {
int roll;
char name[20];
};
int main() {
FILE *fp;
struct Student s;
fp = fopen("student.dat", "rb");
fread(&s, sizeof(s), 1, fp);
printf("%d %s", s.roll, s.name);
fclose(fp);
return 0;
}
π FAQs on File Handling in C
Q1. What is a file?
A collection of data stored permanently on disk.
Q2. How do we open a file?
Using fopen("filename", "mode").
Q3. Difference between text and binary files?
Text files are human-readable, binary files are machine-readable.
Q4. What if file doesnβt exist in "r" mode?
fopen() returns NULL.
Q5. Why use fclose()?
To save data, release resources, and avoid corruption.
π Key Points to Remember
- FILE *fp is mandatory
- Always check fp == NULL
- Use correct file mode
- Always close files using fclose()