二进制写文件与文本文件写文件区别不大
打开方式应为:ios::out | ios::binary
#include<iostream>
#include<fstream>
using namespace std;
class Person
{
public:
char m_Name[64];
int m_Age;
};
void test01()
{
//1、包含头文件
//2、创建流文件
ofstream ofs;
//可将此步和下一步合二为一:ofstream ofs("person.txt",ios::out|ios::binary);
//3、打开文件
ofs.open("person.txt",ios::out|ios::binary);
//4、写文件
Person p = {"Chole",18};
ofs.write((const char *)&p,sizeof(Person))//强制转换为char数据类型
//5、关闭文件
ofs.close();
}
int main()
{
test01();
return 0;
}