File-handling
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>
<fstream> includes two classes for file handling:
ifstream - to read from a file.
ofstream - to create/open and write to a file.
Note: Our online compiler cannot handle file handling right now. So, please install an IDE or text
editor on your computer to run the programs given here.
if (!my_file.is_open()) {
cout << "Error opening the file." << endl;
return 1;
}
3. Using the fail() Function
The fail() function returns
true - if the file failed to open or if it is in a state of error.
false - if the file was opened successfully.
ofstream my_file("example.txt");
if (my_file.fail()) {
cout << "Error opening the file." << endl;
return 1;
}
Note: For simplicity, we recommend using the first method.
Contents of example.txt
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>
using namespace std;
int main() {
// open a text file for writing
ofstream my_file("example.txt");
// check the file for errors
if(!my_file) {
cout << "Error: Unable to open the file." << endl;
return 1;
}
// write multiple lines to the file
my_file << "Line 1" << endl;
my_file << "Line 2" << endl;
my_file << "Line 3" << endl;
// close the file
my_file.close();
return 0;
}
Notice the following code for writing to the file:
my_file << "Line 1" << endl;
my_file << "Line 2" << endl;
my_file << "Line 3" << endl;
This is similar to printing output to a screen:
cout << "Line1" << endl;
In file handling, we just replace cout with the file object to write to the file instead of the console.
Our particular code will write the following text to example.txt:
Line1
Line2
Line3
Note: Writing to an existing file will overwrite the existing contents of the file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// open a text file for appending
ofstream my_file("example.txt", ios::app);
// if the file doesn't open successfully, print an error message
if(!my_file) {
cout << "Failed to open the file for appending." << endl;
return 1;
}
// append multiple lines to the file
my_file << "Line 4" << endl;
my_file << "Line 5" << endl;
my_file << "Line 6" << endl;
// close the file
my_file.close();
return 0;
}
This will add the following lines to example.txt:
Line 4
Line 5
Line 6
Mode Description
ios::app Opens the file and appends new content to it at the end.
int main() {
if (my_file) {
my_file << "This is a test line." << endl;
my_file.close();
}
else {
cout << "Unable to open file for writing." << endl;
return 1;
}
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;
}
// 3. append data to the end of the file
my_file.open("example.txt", ios::app);
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
Read from file: This is a test line.
Read from file:
If we look at the file after running the program, we will find the following contents: