File Handling in C++
Last Updated on: 23rd Dec 2025 15:52:36 PM
In real-world software applications, data should not be lost when a program stops running. Programs often need to store information permanently, such as student records, user login details, transaction history, or configuration settings. This permanent storage of data is achieved using files.
File Handling in C++ is the concept that allows a program to create, read, write, and update files stored on a disk. Instead of keeping data only in memory (which is temporary), file handling helps store data in files so that it can be accessed later, even after the program is closed.
C++ provides powerful file handling support through file stream classes, which make it easy to interact with files in a structured and safe way. File handling is a critical concept in database systems, operating systems, banking software, logging systems, and enterprise applications. Mastering file handling is essential for any programmer who wants to build real-world, professional applications.
What is File Handling in C++?
File handling in C++ refers to the process of managing files by performing operations such as creating a file, writing data into a file, reading data from a file, and closing the file properly. C++ treats files as streams of data, which allows smooth data transfer between the program and the file.
Real-Life Example of File Handling
Consider a student management system in a college. When a student registers, their details are saved in a file. Even after the system is shut down and restarted, the student data remains available. This permanent storage and retrieval of information is made possible using file handling.
File Stream Classes in C++
C++ provides three main file stream classes inside the <fstream> header file.
| Class | Purpose |
|---|---|
ofstream |
Used to write data to a file |
ifstream |
Used to read data from a file |
fstream |
Used to read and write data |
Steps for File Handling in C++
File handling in C++ generally follows a fixed sequence. First, a file is opened. Then, the required read or write operation is performed. Finally, the file is closed to release system resources and ensure data safety.
Writing Data to a File
Writing data to a file means storing information permanently on disk. In C++, this is done using the ofstream class, which creates a file if it does not already exist.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file;
file.open("data.txt");
file << "Welcome to iKeySkills\n";
file << "Learning C++ File Handling";
file.close();
return 0;
}
Output
Data written to file successfully
Explanation of Output
The program creates a file named data.txt and writes the given text into it, after which the file is closed to ensure the data is saved properly.
Reading Data from a File
Reading data from a file means retrieving stored information and displaying or processing it within the program. This is done using the ifstream class.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file;
string line;
file.open("data.txt");
while (getline(file, line)) {
cout << line << endl;
}
file.close();
return 0;
}
Output
Welcome to iKeySkills
Learning C++ File Handling
Explanation of Output
The program reads each line from the file and displays it on the screen until the end of the file is reached.
Using fstream for Read and Write
The fstream class allows both reading and writing operations on the same file, which is useful for updating file content.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file;
file.open("info.txt", ios::out);
file << "File handling in C++";
file.close();
file.open("info.txt", ios::in);
string text;
getline(file, text);
cout << text << endl;
file.close();
return 0;
}
Output
File handling in C++
Explanation of Output
The program first writes text into the file and then reopens the same file to read and display the stored content.
File Opening Modes
File modes specify how a file should be opened. They control whether data is read, written, or appended.
| Mode | Description |
|---|---|
ios::in |
Opens file for reading |
ios::out |
Opens file for writing |
ios::app |
Appends data at the end |
ios::binary |
Opens file in binary mode |
Appending Data to a File
Appending means adding new data to the end of an existing file without deleting its previous content.
Example
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file;
file.open("data.txt", ios::app);
file << "\nAdvanced C++ Concepts";
file.close();
return 0;
}
Output
Data appended successfully
Explanation of Output
The program adds new text at the end of the existing file while keeping the old data unchanged.
Advantages of File Handling
File handling allows permanent data storage, supports large data processing, enables data sharing between programs, and improves application reliability.
Disadvantages of File Handling
File operations can be slower than memory operations, improper file handling may lead to data corruption, and incorrect file closing can cause resource leaks.
Real-World Applications of File Handling
File handling is used in banking systems, student databases, log files, configuration files, report generation systems, and content management systems.
Conclusion
File handling in C++ is a fundamental concept for building real-world applications that require permanent data storage. It enables programs to store, retrieve, and manage data efficiently using files. By understanding file streams, file modes, and proper file operations, programmers can develop reliable and professional applications. Mastery of file handling is essential for database-driven and enterprise-level software development.
Keep practicing — you're doing amazing!
Happy Coding! ![]()