The data generated when the program is running are all temporary data, and will be released once the program is finished.
Data can be persisted through files
File operations in C++ need to include header files < fstream >
There are two types of files:
- Text file -the file is stored in the computer in the form of text ASCII code
- Binary files -files are stored in the computer in binary form of text , and users generally cannot read them directly
There are three main categories of operating files:
- ofstream: write operation
- ifstream: read operation
- fstream: read and write operations
1. Text file
1. Write files
The steps for writing files are as follows:
-
Include header file
#include <fstream>
-
Create a stream object
ofstream ofs;
-
open a file
ofs.open("file path", open method);
-
Write data
ofs << "Data written";
-
Close file
ofs.close();
File opening method:
Open method | Explanation |
---|---|
ios::in | Open file for reading |
ios::out | Open file for writing |
ios::ate | Initial position: end of file |
ios::app | Write file in append mode |
ios::trunc | If the file exists, delete it first, then create it |
ios::binary | Binary way |
Note: The file opening method can be used in conjunction with the | operator
For example: write files in binary modeios::binary | ios:: out
#include <fstream>
void test01()
{
ofstream ofs;
ofs.open("test.txt", ios::out);
ofs << "姓名:张三" << endl;
ofs << "性别:男" << endl;
ofs << "年龄:18" << endl;
ofs.close();
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- File operations must include the header file fstream
- Reading files can use ofstream, or fstream class
- When opening the file, you need to specify the path of the operation file and the opening method
- Use << to write data to the file
- After the operation is complete, close the file
2. Read the file
Reading files is similar to writing files, but there are more ways to read
The steps to read the file are as follows:
-
Include header file
#include <fstream>
-
Create a stream object
ifstream ifs;
-
Open the file and determine whether the file is opened successfully
ifs.open("file path", open method);
-
Read data
Four ways to read
-
Close file
ifs.close();
#include <fstream>
#include <string>
void test01()
{
ifstream ifs;
ifs.open("test.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//第一种方式
//char buf[1024] = { 0 };
//while (ifs >> buf)
//{
// cout << buf << endl;
//}
//第二种
//char buf[1024] = { 0 };
//while (ifs.getline(buf,sizeof(buf)))
//{
// cout << buf << endl;
//}
//第三种
//string buf;
//while (getline(ifs, buf))
//{
// cout << buf << endl;
//}
char c;
while ((c = ifs.get()) != EOF)
{
cout << c;
}
ifs.close();
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- Reading files can use ifstream or fstream class
- Use the is_open function to determine whether the file is opened successfully
- close close file
Second, the binary file
Read and write files in binary mode
The opening method should be specified as ios::binary
1. Write files
The binary method of writing files 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 section of storage space in the memory. len is the number of bytes read and written
#include <fstream>
#include <string>
class Person
{
public:
char m_Name[64];
int m_Age;
};
//二进制文件 写文件
void test01()
{
//1、包含头文件
//2、创建输出流对象
ofstream ofs("person.txt", ios::out | ios::binary);
//3、打开文件
//ofs.open("person.txt", ios::out | ios::binary);
Person p = {
"张三" , 18};
//4、写文件
ofs.write((const char *)&p, sizeof(p));
//5、关闭文件
ofs.close();
}
int main() {
test01();
system("pause");
return 0;
}
to sum up:
- The file output stream object can write data in binary mode through the write function
2. Read the file
The binary mode of reading files 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 section of storage space in the memory. len is the number of bytes read and written
#include <fstream>
#include <string>
class Person
{
public:
char m_Name[64];
int m_Age;
};
void test01()
{
ifstream ifs("person.txt", ios::in | ios::binary);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
}
Person p;
ifs.read((char *)&p, sizeof(p));
cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}
int main() {
test01();
system("pause");
return 0;
}
The file input stream object can read the data in binary mode through the read function