In C++, file operations are mainly implemented through three classes: ifstream
(input file stream) is used to read data from files; ofstream
(output file stream) is used to write data to files; fstream
(file stream) can perform both read and write operations. To use these classes, we need to include the <fstream>
header file.
Opening Files
Before performing read and write operations on a file, we need to open it first. Taking ofstream
as an example, opening a file is very simple:
#include <iostream>
#include <fstream>
int main() {
ofstream outFile("example.txt");
if (outFile.is_open()) {
outFile << "This is the content written to the file";
outFile.close();
} else {
std::cout << "Cannot open file";
}
return 0;
}
In this code, we create an ofstream
object outFile
and try to open a file named example.txt
. If the file opens successfully, we write some content to the file and then close it. If the file cannot be opened, we output an error message.
The usage of ifstream
is similar, but it is used for reading files. For example:
#include <iostream>
#include <fstream>
#include <string>
int main() {
ifstream inFile("example.txt");
if (inFile.is_open()) {
std::string line;
while (std::getline(inFile, line)) {
std::cout << line << std::endl;
}
inFile.close();
} else {
std::cout << "Cannot open file";
}
return 0;
}
In this code, we use ifstream
to open the file and read its contents line by line using the getline
function, outputting it to the console.
File Modes
When opening a file, we can specify the file mode. Common file modes include:
-
•
ios::in
: Opens the file in read mode. -
•
ios::out
: Opens the file in write mode, overwriting existing content if the file exists. -
•
ios::app
: Opens the file in append mode, writing data at the end of the file. -
•
ios::binary
: Opens the file in binary mode, used for handling non-text files such as images and audio.
For example, we can open a file in append mode like this:
ofstream outFile("example.txt", ios::app);
Closing Files
After completing file operations, it is important to remember to close the file. Closing a file releases system resources and ensures data integrity. We can use the close
member function to close the file, as shown in the previous examples.
Random Access Files
C++ also supports random access to files. We can use the seekg
(for input streams) and seekp
(for output streams) functions to move the file pointer to a specified position. For example, to move the input file pointer to the end of the file, we can do this:
inFile.seekg(0, ios::end);
This moves the file pointer to the end of the file, where 0
represents the offset and ios::end
indicates that the offset is calculated from the end of the file.
By mastering C++ file and stream operations, we can effectively interact with external files, achieving persistent data storage and retrieval. This is crucial for developing various types of applications. In subsequent studies, we can also delve into more advanced features of file operations, such as complex read and write operations for binary files.