C++ files and streams
So far, we have used the iostream standard library, which provides the cin and cout methods for reading streams from standard input and Writes a stream to standard output.
This tutorial explains how to read streams from files and write streams to files. This requires the use of another standard library in C++ fstream, which defines three new data types:
Data type | Description |
---|---|
ofstream | This data type represents the output file stream, used to create files and write information to files. |
ifstream | This data type represents the input file stream and is used to read information from the file. |
fstream | This data type usually represents a file stream and has both ofstream and ifstream functions, which means it can create files and write information to files , reads information from a file. |
To do file processing in C++, the header files <iostream> and <fstream> must be included in the C++ source code file.
Open the file
Before reading information from the file or writing information to the file, the file must be opened first. Both the ofstream and fstream objects can be used to open files for writing operations. If you only need to open the file for reading operations, use the ifstream object.
The following is the standard syntax of the open() function, which is a member of the fstream, ifstream and ofstream objects.
void open(const char *filename, ios::openmode mode);
Here, open() The first parameter of the member function specifies the name and location of the file to be opened, and the second parameter defines the mode in which the file is opened.
Mode Flags | Description |
---|---|
ios::app | Append mode. All writes are appended to the end of the file. |
ios::ate | After the file is opened, navigate to the end of the file. |
ios::in | Open the file for reading. |
ios::out | Open the file for writing. |
ios::trunc | If the file already exists, its content will be truncated before opening the file, that is, the file length will be set to 0. |
You can combine the above two or more modes. For example, if you want to open a file in write mode and want to truncate the file in case the file already exists, then you can use the following syntax:
ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc );
Similarly, if you want to open a file with For reading and writing, you can use the following syntax:
fstream afile; afile.open("file.dat", ios::out | ios::in );
Close the file
When the C++ program terminates, it will automatically close and refresh all streams, release all allocated memory, and close all open document. But programmers should develop a good habit of closing all open files before the program terminates.
The following is the standard syntax of the close() function. The close() function is a member of the fstream, ifstream and ofstream objects.
void close();
Writing to a file
In C++ programming, we use the stream insertion operator (<<) to write information to a file, just like using this operator to output information to the screen Same. The only difference is that here you are using a ofstream or fstream object instead of a cout object.
Reading files
In C++ programming, we use the stream extraction operator ( >> ) to read information from a file, just like we use this operator to enter information from the keyboard. The only difference is that here you are using an ifstream or fstream object instead of a cin object.
Read & Write Example
The following C++ program opens a file in read-write mode. After writing the user-entered information to the file afile.dat, the program reads the information from the file and outputs it to the screen:
#include <fstream> #include <iostream> using namespace std; int main () { char data[100]; // 以写模式打开文件 ofstream outfile; outfile.open("afile.dat"); cout << "Writing to the file" << endl; cout << "Enter your name: "; cin.getline(data, 100); // 向文件写入用户输入的数据 outfile << data << endl; cout << "Enter your age: "; cin >> data; cin.ignore(); // 再次向文件写入用户输入的数据 outfile << data << endl; // 关闭打开的文件 outfile.close(); // 以读模式打开文件 ifstream infile; infile.open("afile.dat"); cout << "Reading from the file" << endl; infile >> data; // 在屏幕上写入数据 cout << data << endl; // 再次从文件读取数据,并显示它 infile >> data; cout << data << endl; // 关闭打开的文件 infile.close(); return 0; }
When the above code is compiled and executed, it produces the following Input and output:
$./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara 9
The above example uses additional functions of the cin object. For example, the getline() function reads a line from the outside, and the ignore() function ignores the extra characters left by the previous reading statement.
File location pointer
istream and ostream both provide member functions for relocating the file location pointer. These member functions include seekg ("seek get") for istream and seekp ("seek put") for ostream.
The argument to seekg and seekp is usually a long integer. The second parameter can be used to specify the search direction. The search direction can be ios::beg (default, starting from the beginning of the stream), or ios::cur (starting from the current position of the stream), or Can be ios::end (positioned from the end of the stream).
The file position pointer is an integer value that specifies the number of bytes from the starting position of the file to the position of the pointer. Here is an example of positioning the "get" file location pointer:
// 定位到 fileObject 的第 n 个字节(假设是 ios::beg) fileObject.seekg( n ); // 把文件的读指针从 fileObject 当前位置向后移 n 个字节 fileObject.seekg( n, ios::cur ); // 把文件的读指针从 fileObject 末尾往回移 n 个字节 fileObject.seekg( n, ios::end ); // 定位到 fileObject 的末尾 fileObject.seekg( 0, ios::end );