File handling in C++ allows you to work with files by reading, writing, or modifying their contents. The file handling operations in C++ are performed using the fstream library, which provides facilities for both input and output operations on files.
File Classes
1. ifstream: Used for reading data from a file (input file stream).
2. ofstream: Used for writing data to a file (output file stream).
3. fstream: Used for both reading and writing data to a file.
File Modes
When opening a file, you specify the mode in which you want to open it. These modes are passed as flags to the open() function.
ios::in Open a file for reading.
ios::out Open a file for writing.
ios::app Append to the file (writing at the end).
ios::trunc Truncate the file to zero length if it exists.
ios::binary Open the file in binary mode.
1. Reading a File
To reads a file line by line using ifstream and prints its content to the console.
Example:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Open the file for reading
ifstream myFile("example.txt");
// Check if the file was successfully opened
if (!myFile) {
cerr << "Error opening the file for reading!" << endl;
return 1;
}
string line;
while (getline(myFile, line)) {
cout << line << endl; // Print each line from the file
}
myFile.close(); // Close the file after reading
return 0;
}
Explanation:
- The program opens example.txt for reading.
- It checks if the file was successfully opened.
- It reads the file line by line using getline() and prints the content.
- Finally, the file is closed.
2. Writing to a File
It writes text to a file using ofstream. It creates a new file and writes data to it.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Open the file for writing
ofstream myFile("output.txt");
// Check if the file was successfully opened
if (!myFile) {
cerr << "Error opening the file for writing!" << endl;
return 1;
}
// Write data to the file
myFile << "Hello, Friends!" << endl;
myFile << "This is a example of writing a file." << endl;
myFile << "End the line" << endl;
myFile.close(); // Close the file after writing
cout << "Data written to output.txt successfully." << endl;
return 0;
}
Explanation:
- The program opens output.txt for writing.
- It checks if the file was successfully opened.
- It writes a few lines of text to the file using the << operator.
- Finally, the file is closed.
Note: If output.txt already exists, it will be truncated (overwritten) unless you open it in append mode.
3. Appending to a File
It appends new data to an existing file using ofstream with the ios::app mode.
Example:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Open the file in append mode
ofstream myFile("output.txt", ios::app);
// Check if the file was successfully opened
if (!myFile) {
cerr << "Error opening the file for appending!" << endl;
return 1;
}
// Append new data to the file
myFile << "This line is appended to the file." << endl;
myFile << "Appending more data to the file." << endl;
myFile.close(); // Close the file after appending
cout << "Data appended to output.txt successfully." << endl;
return 0;
}
Explanation:
- The program opens output.txt in append mode using ios::app.
- It checks if the file was successfully opened.
- It appends two new lines of text to the existing file.
- Finally, the file is closed.
Note: If output.txt doesn't exist, it will be created.