c++二进制读写文件
#include<iostream>
#include<fstream>
using namespace std;
class Person
{
public:
char m_Name[64];
int m_Age;
};
void text01()
{
ofstream ofs("person.txt",ios::out|ios::binary);
Person p={"cw",18};
ofs.write((const char *)&p,sizeof(Person));
ofs.close();
}
void text02()
{
ifstream ifs;
ifs.open("person.txt",ios::in|ios::binary);
if(!ifs.is_open())
{
cout<<"open failed"<<endl;
return ;
}
Person p;
ifs.read((char *)&p,sizeof(Person));
cout<<p.m_Age<<" "<<p.m_Name<<endl;
}
int main()
{
text01();
text02();
return 0;
}