Jan 22, 2026
## Chapter 11: File Handling
All the programs we've written so far: Their data was lost as soon as the program ended. But this doesn't work in real-life programs. Many times we need to save data and read it later. This is where C gives us 👉 File Handling.
What is File Handling? (Simple Language)
File Handling means writing, reading, or appending data to a file through a program. Data remains safe even after the program closes.
Opening and Closing Files
To work with files in C:
* A `FILE *` pointer is used
* `fopen()` → to open a file
* `fclose()` → to close a file
Practical Example: Writing Data to a File
```c
#include
int main() {
FILE *filePtr;
// Opening the file in write mode
filePtr = fopen("info.txt", "w");
// Error check
if (filePtr == NULL) {
printf("Could not open the file!n");
return 1;
}
// Writing data to the file
fprintf(filePtr, "Hello!n");
fprintf(filePtr, "This is an example of C file handling.n");
// Closing the file
fclose(filePtr);
printf("Data successfully written to the file.n");
return 0;
}
```
Now let's understand this in simple language:
1. `FILE *filePtr;`: `FILE` is C's built-in structure; `filePtr` is a pointer that controls the file.
2. `fopen()`: opens the file. "w" means write mode.
3. Error check: `if (filePtr == NULL)`.
4. `fprintf()`: writes to a file instead of the screen.
5. `fclose()`: saves data properly and prevents leaks.
File Modes in C
| Mode | Meaning |
| :--- | :--- |
| "r" | For reading (file must exist) |
| "w" | For writing (new or overwrite) |
| "a" | For appending to the end of the file |
| "r+" | Read + Write |
| "w+" | Write + Read |
| "a+" | Append + Read |
Reading from a File
So far, we have learned to write data. Now, to read previously saved data and display it on the screen, we use read mode ("r").
Practical Example: Reading the entire contents of a file
```c
#include
int main() {
FILE *reader;
char buffer[120];
// Opening the file in read mode
reader = fopen("info.txt", "r");
if (reader == NULL) {
printf("Could not open the file!n");
return 1;
}
printf("File contents:nn");
// Reading line by line
while (fgets(buffer, 120, reader) != NULL) {
printf("%s", buffer);
}
fclose(reader);
return 0;
}
```
1. `fopen("info.txt", "r")`: read mode.
2. `fgets()`: reads one line, returns NULL at EOF.
3. while loop: necessary to read line by line.
Important Point: If the file does not exist, `fopen()` returns `NULL`.
Complete Example: Student Database (File + Structure)
A small database system: saving student data in a file and reading it later.
```c
#include
#include
// student structure
struct Record {
char studentName[50];
int rollNo;
float score;
};
// function to add a student record
void addRecord() {
FILE *fp = fopen("records.txt", "a");
struct Record r;
if (fp == NULL) {
printf("Could not open the file!n");
return;
}
printf("Enter name: ");
scanf(" %[^n]", r.studentName);
printf("Enter roll number: ");
scanf("%d", &r.rollNo);
printf("Enter score: ");
scanf("%f", &r.score);
// Writing to the file
fprintf(fp, "%s,%d,%.2fn", r.studentName, r.rollNo, r.score);
fclose(fp);
printf("Record added successfully!n");
}
// Function to display all students
void showRecords() {
FILE *fp = fopen("records.txt", "r");
struct Record r;
if (fp == NULL) {
printf("No records exist!n");
return;
}
printf("nStudent Records:n");
printf("%-20s %-10s %-10sn", "Name", "Roll", "Score");
printf("---------------------------------------n");
// Reading CSV format
while (fscanf(fp, "%[^,],%d,%fn", r.studentName, &r.rollNo, &r.score) != EOF) {
printf("%-20s %-10d %-10.2fn", r.studentName, r.rollNo, r.score);
}
fclose(fp);
}
int main() {
int option;
do {
printf("n1. Add new studentn");
printf("2. View all studentsn");
printf("3. Exitn");
printf("Choose your option: ");
scanf("%d", &option);
switch(option) {
case 1: addRecord(); break;
case 2: showRecords(); break;
case 3: printf("Program terminated!n"); break;
default: printf("Invalid option selected!n");
}
} while (option != 3);
return 0;
}
```
---
## Conclusion
In this article, we explored the core concepts of Explore blog under C language - File Handling. Understanding these fundamentals is crucial for any developer looking to master this topic.
## Frequently Asked Questions (FAQs)
**Q: What is Explore blog under C language - File Handling?**
A: Explore blog under C language - File Handling is a fundamental concept in this programming language/topic that allows developers to perform specific tasks efficiently.
**Q: Why is Explore blog under C language - File Handling important?**
A: It helps in organizing code, improving performance, and implementing complex logic in a structured way.
**Q: How to get started with Explore blog under C language - File Handling?**
A: You can start by practicing the basic syntax and examples provided in this tutorial.
**Q: Are there any prerequisites for Explore blog under C language - File Handling?**
A: Basic knowledge of programming logic and syntax is recommended.
**Q: Can Explore blog under C language - File Handling be used in real-world projects?**
A: Yes, it is widely used in enterprise-level applications and software development.
**Q: Where can I find more examples of Explore blog under C language - File Handling?**
A: You can check our blog section for more advanced tutorials and use cases.
**Q: Is Explore blog under C language - File Handling suitable for beginners?**
A: Yes, our guide is designed to be beginner-friendly with clear explanations.
**Q: How does Explore blog under C language - File Handling improve code quality?**
A: By providing a standardized way to handle logic, it makes code more readable and maintainable.
**Q: What are common mistakes when using Explore blog under C language - File Handling?**
A: Common mistakes include incorrect syntax usage and not following best practices, which we've covered here.
**Q: Does this tutorial cover advanced Explore blog under C language - File Handling?**
A: This article covers the essentials; stay tuned for our advanced series on this topic.