Write函数需要写入地址,所以需要传入&p,但是代码定义为类,需将其地址转换为字符型,强制类型转换

 

# include<iostream>
# include<fstream>

using namespace std;

class Person
{
public:
	char m_Name[64];//姓名
	int m_Age; //年龄
};

void test01()
{
	//包含头文件
	//创建流对象
	ofstream ofs;
	//打开文件
	ofs.open("person.txt",ios::out|ios::binary);
	//写文件
	Person p = {"张三",18};
	ofs.write((const char *)&p,sizeof(Person));
	//关闭文件
	ofs.close();
}
int main()
{
	test01();
	system("pause");
	return 0;
}