5.1 text documents
5.1.1 writing documents
The steps for writing files are as follows:
-
Include header file
#include <fstream>
-
Create flow object
ofstream ofs;
-
Open file
ofs.open("file path", opening method);
-
Write data
Ofs < < "written data";
-
Close file
ofs.close();
File opening method:
Open mode | explain |
---|---|
ios::in | Open file for reading |
ios::out | Open file for writing |
ios::ate | Initial location: end of file |
ios::app | Write file in append mode |
ios::trunc | If the file exists, delete it before creating it |
ios::binary | Binary mode |
Note: the file opening method can be used in conjunction with the | operator
For example, write the file IOS:: binary | IOS:: out in binary mode
Example:
#include <iostream> using namespace std; #include <fstream> // Text file write file void test01() { // 1. Include header file fstream // 2. Create flow object ofstream ofs; // 3. Specify how to open ofs.open("test.txt", ios::out); // 4. Write content ofs << "Name: Zhang San" << endl; ofs << "Gender: Male" << endl; ofs << "Age: 18" << endl; // 5. Close file ofs.close(); } int main() { system("pause"); return 0; }
Summary:
- The file operation must contain the header file fstream
- You can use ofstream or fsstream class to read files
- When opening a file, you need to specify the path of the operation file and the opening method
- Use < < to write data to a file
- After the operation, close the file
5.1.2 document reading
Reading a file is similar to writing a file, but there are many ways to read it
The steps for reading files are as follows:
-
Include header file
#include <fstream>
-
Create flow object
ifstream ifs;
-
Open the file and judge whether the file is opened successfully
ifs.open("file path", opening method);
-
Read data
Four ways to read
-
Close file
ifs.close();
Example:
#include <iostream> using namespace std; #include <fstream> #include <string> // Text file read file void test01() { // 1. Include header file // 2. Create flow object ifstream ifs; // 3. Open the file and judge whether it is opened successfully ifs.open("test.txt", ios::in); if (!ifs.is_open()) { cout << "File open failed" << endl; return; } // 4. Read data // First kind //char buf[1024] = { 0 }; //while ( ifs >> buf ) //{ // cout << buf << endl; //} // Second //char buf[1024] = { 0 }; //while ( ifs.getline(buf, sizeof(buf)) ) //{ // cout << buf << endl; //} // Third //string buf; //while ( getline(ifs, buf) ) //{ // cout << buf << endl; //} // Fourth //char c; //while ( (c = ifs.get()) != EOF ) // EOF end of file //{ // cout << c; //} // 5. Close file ifs.close(); } int main() { test01(); system("pause"); return 0; }
Summary:
- I fstream or fsstream class can be used to read files
- Using is_ The open function can judge whether the file is opened successfully
- Close close file
5.2 binary files
Read and write files in binary mode
The opening method should be specified as ios::binary
5.2.1 writing documents
Binary file writing mainly uses the stream object to call the member function write
Function prototype: ostream & write (const char * buffer, int len);
Parameter explanation: the character pointer buffer points to a storage space in memory. len is the number of bytes read and written
Example:
#include <iostream> using namespace std; #include <fstream> class Person { public: char m_Name[64]; // full name int m_Age; // Age }; void test01() { // 1. Include header file // 2. Create flow object ofstream ofs("Person.txt", ios::out | ios::binary); // 3. Open file // ofs.open("Person.txt", ios::out | ios::binary); // 4. Write file Person p = { "Zhang San", 18 }; ofs.write((const char*)&p, sizeof(Person)); // 5. Close file ofs.close(); } int main() { test01(); system("pause"); return 0; }
Summary:
- The file output stream object can write data in binary mode through the write function
5.2.2 document reading
Binary file reading mainly uses the stream object to call the member function read
Function prototype: istream & read (char * buffer, int len);
Parameter explanation: the character pointer buffer points to a storage space in memory. len is the number of bytes read and written
Example:
#include <iostream> using namespace std; #include <fstream> class Person { public: char m_Name[64]; // full name int m_Age; // Age }; // Binary file read file void test01() { // 1. Include header file // 2. Create flow object fstream ifs; // 3. Open the file to judge whether the file is opened successfully ifs.open("Person.txt", ios::in | ios::binary); if (!ifs.is_open()) { cout << "File open failed" << endl; return; } // 4. Read file Person p; ifs.read((char*)&p, sizeof(Person)); cout << "full name:" << p.m_Name << endl; cout << "Age:" << p.m_Age << endl; // 5. Close file ifs.close(); } int main() { test01(); system("pause"); return 0; }
- The file input stream object can read data in binary mode through the read function