File Handling in C++: Read and Write Operations

Introduction
File handling is one of the most important programming paradigms. It allows us to save the data permanently on the disk. File handling in C++ is accomplished by including classes from the standard library made for the easy reading and writing of data into files.
This guide will expose you to the file handling concept, how it works in C++, and how you can read and write data from files with the help of an example. It should suit beginners at all levels in file handling and be a resource for students and budding programmers.
What is File Handling in C++?
File handling in C++ operates by opening a file, reading a file, writing a file, and closing it. C++ provides a set of classes in the <fstream> header to manage file operations like opening, reading, writing, and closing files.
ifstream- It refers to the input file system. It is responsible for reading the data from files and displaying it.
ofstream- It stands for the Output File system. It is responsible for sending data from your program to a file.
fstream- It refers to the File Stream. It is used to open an input and output file.
Why File Handling is Important?
File handling is important as it allows a program to store data permanently. When your data is not saved in a file, it exits your data in temporary mode. If the system crashes, your data is deleted automatically.
Sometimes, it need to store data in the system permanently for future retrieval. In other words, files provide long-term storage on a disk, enabling programs to save, retrieve, and update data even after the program ends.
C++ File Open:
Generally, files must be opened before any activity is performed on them. In C++, an opened file can be in two forms, either using the object of an ifstream, ofstream, or fstream.
Syntax: 1
ofstream myFile;
myFile.open("data.txt");
Syntax: 2
ifstream myFile;
myFile.open("data.txt");
You can also open files directly while declaring the object:
ofstream myFile("data.txt"); // for writing
ifstream myFile("data.txt"); // for reading
Writing to a File in C++:
ofstream class is used to perform writing into the file in C++.
Example:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream myFile("example.txt");
if (myFile.is_open()) {
myFile << "Hello, this is a file handling example in C++.\n";
myFile << "We are writing this text to the file.";
myFile.close();
} else {
cout << "Unable to open the file." << endl;
}
return 0;
}
Output:
Unable to open the file.
Explanation:
· ofstream myFile("example.txt"); opens the file.
· myFile.close(); closes the file after writing.
Reading from a File in C++
We use the ifstream class to read data from a file. Here is an example:
Example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream myFile("example.txt");
string line;
if (myFile.is_open()) {
while (getline(myFile, line)) {
cout << line << endl;
}
myFile.close();
} else {
cout << "Unable to open the file." << endl;
}
return 0;
}
Output:
Unable to open the file.
Read and Write Together Using Fstream
If the user wants to perform both read and write operations, you can use the fstream file class in program.
Example:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream myFile;
myFile.open("data.txt", ios::in | ios::out);
if (myFile.is_open()) {
myFile << "Adding more data to the file.\n";
myFile.seekg(0);
string line;
while (getline(myFile, line)) {
cout << line << endl;
}
myFile.close();
} else {
cout << "Unable to open the file." << endl;
}
return 0;
}
Output:
Unable to open the file.
Note:
· ios::in- It means input mode (read).
· ios::out- It means output mode (write).
File Opening Modes
File Opening Modes are shown below:
1) ios::app - Append mode: Add new data from the file without deleting existing data
2) ios:trunc -Truncate mode: this mode is known as the default mode and is used for output files.
3) ios:binary – it opens the file in only binary format (0 and 1). This is used for only non-text data such as images, videos etc.
4) ios:ate – it refers to the at-end mode. Basically, this is used to remove the file at the end of the file.
Example:
ofstream myFile("example.txt", ios::app); // opens in append mode
Closing a File:
It is best practice to close a file at the end of a read or write using:
myFile.close();
This frees the memory that the program was using for that file and ensures a safe, correct file save operation.
Common Errors in File Handling
1. File not opening: Verify the file's existence (in case of reading) or that you have the right permissions to write to it.
2. Wrong path: if you are not using the correct file name and path, then file is not open. Always check that the file name and its path are correct before try to open or read the file.
3. Not closing the file: The file opens properly using the .close() function.
Conclusion
File handling in C++ is the most importance to store and manage data permanently. You can handle data in files just like a professional by learning about the basic syntax and functions.
In the article, File Handling in C++: Read and Write Operations, I suggest you learn C++ Programming language from the Tpoint Tech website as this website provides C++ tutorials, interview questions, and an Online Compiler as well.
File handling allows users to keep their data safe even after the program ends. Users can design a student record system or save logs for a program. Small file operations may start, and with practice, users will manage more complex file tasks easily.
What's Your Reaction?






