C++ File Handling
C++ File Handling
File handling in C++ is a mechanism to create and perform read/write operations on a file.
We can access various file handling methods in C++ by importing the <fstream> class.
#include <fstream>
std::ofstream my_file("example.txt");
Here,
Note: We can also use the open() function to open a file. For example,
std::ofstream my_file.open("example.txt");
Closing a File
Once we're done working with a file, we need to close it using the close() function.
my_file.close();
#include <iostream>
#include <fstream>
int main() {
return 0;
}
Note: If there's no such file to open, ofstream my_file("example.txt"); will instead create a new file
named example.txt .
if (!my_file) {...}
This method checks if the file is in an error state by evaluating the file object itself.
If the file has been opened successfully, the condition evaluates to true .
If there's an error, it evaluates to false , and you can handle the error accordingly.
For example,
ofstream my_file("example.txt");
if (!my_file.is_open()) {
cout << "Error opening the file." << endl;
return 1;
}
ofstream my_file("example.txt");
if (my_file.fail()) {
cout << "Error opening the file." << endl;
return 1;
}
Then, we need to read the file line-by-line. To do this, we need to loop through each line of the file until all the
lines are read, i.e., until we reach the end of the file.
false - if the file pointer doesn't point to the end of the file
For example,
Here, the while loop will run until the end of the file. In each iteration of the loop,
getline(my_file, line); reads the current line of the file and stores it in the line variable.
return 0;
}
Hello, World!
How are you?
Write to a File
We use the ofstream class to write to a file. For example,
ofstream my_file("example.txt");
We can then write to the file by using the insertion operator << with the ofstream object my_file . For
example,
#include <iostream>
#include <fstream>
int main() {
return 0;
}
In file handling, we just replace cout with the file object to write to the file instead of the console.
Line1
Line2
Line3
Note: Writing to an existing file will overwrite the existing contents of the file.
In C++, you can achieve this by using the ios::app flag when opening the file:
Now, let's add some more text to the existing content of example.txt :
#include <iostream>
#include <fstream>
int main() {
return 0;
}
Line 4
Line 5
Line 6
Contents Appended to an Existing File
File Handling With fstream
Instead of using ifstream to read from a file and ofstream to write to the file, we can simply use the fstream
The constructor for fstream allows you to specify the file name and the mode for file operations.
Mode Description
ios::app Opens the file and appends new content to itat the end.
if (my_file) {
while (!my_file.eof()) {
getline(my_file, line);
cout << "Read from file: " << line << endl;
}
my_file.close();
}
else {
cout << "Unable to open file for reading." << endl;
return 1;
}
if (my_file) {
my_file << "This is another test line, appended to the file." << endl;
my_file.close();
}
else {
cout << "Unable to open file for appending." << endl;
return 1;
}
return 0;
}
Output
If we look at the file after running the program, we will find the following contents:
File Handling Using fstream
Note: Using ifstream and ofstream explicitly signifies the intention of reading or writing, respectively,
making the code more readable and less prone to errors. Using fstream for both might lead to
ambiguity or unintended operations if not handled carefully.