Unit 10: File Handling in C Programming | B. Sc Year: III
🧭 Introduction
So far, all the C programs you wrote lost their data once the program ended.
But in real-world applications, data must be stored permanently so it can be used again later.
This is where file handling comes in.
Using file handling, C programs can:
Store data permanently on disk
Read data whenever needed
Update, append, or manage records
File handling is widely used in:
Student record systems
Banking software
Inventory management
Report generation
This unit is very important for exams, especially TU long questions and programs.
🔑 Key Concepts (Simple Explanation)
1️⃣ What is File Handling?
File handling is the process of:
Creating files
Writing data into files
Reading data from files
A file is a collection of data stored permanently on secondary storage (hard disk).
Exam Definition ✅
File handling in C is a technique used to store and retrieve data permanently using files.
2️⃣ Types of Files in C
C supports two main types of files:
🔹 Text Files
Data stored as characters
Human-readable
Example:
.txtfiles
✅ Used for reports, logs, plain text
🔹 Binary Files
Data stored in binary form (0s and 1s)
Faster and more efficient
Not human-readable
✅ Used for databases, records, images
3️⃣ FILE Pointer
C uses a FILE pointer to access files.
Declaration:
FILE *fp;
✅ FILE is defined in <stdio.h>
4️⃣ Opening and Closing a File
Open a file using fopen()
fp = fopen("file.txt", "mode");
Close a file using fclose()
fclose(fp);
✅ Always close files to prevent data loss.
5️⃣ File Opening Modes
| Mode | Meaning |
|---|---|
"r" | Read |
"w" | Write (creates new file) |
"a" | Append |
"r+" | Read & write |
"w+" | Write & read |
"a+" | Append & read |
✅ Mode questions are very common in exams.
6️⃣ Writing Data to a File
Using fprintf()
fprintf(fp, "Hello File");
Program Example: Write Data
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "w");
fprintf(fp, "Welcome to File Handling in C!");
fclose(fp);
return 0;
}
✅ Creates and writes into data.txt.
7️⃣ Reading Data from a File
Using fscanf()
fscanf(fp, "%s", data);
Program Example: Read Data
#include <stdio.h>
int main() {
FILE *fp;
char ch;
fp = fopen("data.txt", "r");
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
✅ EOF marks the end of file.
8️⃣ Character I/O File Functions
| Function | Purpose |
|---|---|
fgetc() | Read one character |
fputc() | Write one character |
Example:
fputc('A', fp);
char ch = fgetc(fp);
9️⃣ File Handling with Structures
Example: Write Structure to File
struct student {
int roll;
char name[20];
};
fwrite(&s, sizeof(s), 1, fp);
✅ Often asked as an advanced exam question.
🔟 Binary File Functions
| Function | Use |
|---|---|
fread() | Read binary data |
fwrite() | Write binary data |
Syntax:
fwrite(&data, sizeof(data), 1, fp);
fread(&data, sizeof(data), 1, fp);
1️⃣1️⃣ Error Handling in Files
Always check if the file opened successfully:
if (fp == NULL) {
printf("File cannot be opened");
return 1;
}
✅ Gives extra safety and marks.
🔍 Difference Between Text File and Binary File
| Text File | Binary File |
|---|---|
| Human readable | Not readable |
| Slower | Faster |
| Stored as characters | Stored as raw data |
| Easy to edit | More secure |
✅ Frequently asked in theory questions.
📝 Important Exam Questions
What is file handling in C?
Types of files in C
Explain file opening modes
Write a program to write and read a file
Difference between text file and binary file
What is FILE pointer?
Explain
fopen()andfclose()Explain
fread()andfwrite()
🎯 Exam Tips / Key Points
Always include
<stdio.h>Use correct file mode
Close file after use
Check for
NULLpointerUse
EOFwhile readingPrograms from this unit are high scoring
✅ Short Summary
In Unit 10, you learned:
What file handling is and why it is needed
Types of files (text and binary)
FILE pointer and file modes
File input/output functions
Writing and reading files
Error handling in files
File handling allows programs to store data permanently, making them suitable for real-world applications.
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...