#include <iostream>
#include <fstream>
using namespace std;
class Dog
{
public:
Dog(int a,int b):weight(a),age(b){}
~Dog(){}
int getWeight(){return weight;}
int getAge(){return age;}
void setWeight(int w){weight = w;}
void setAge(int age){(*this).age = age;}
private:
int weight,age;
};
int main()
{
ofstream fout("1.txt",ios_base::out|ios_base::binary);
Dog dog1(5,10);
fout.write((char*)&dog1,sizeof dog1);
fout.close();
ifstream fin("1.txt",ios_base::in|ios_base::binary);
Dog dog2(2,2);
cout <<dog2.getWeight() << "\n" << dog2.getAge() <<endl;
fin.read((char*)&dog2,sizeof dog2);
cout <<dog2.getWeight() << "\n" << dog2.getAge() <<endl;
fin.close();
return 0;
}
文件名部分也可以考虑用char filename[]来实现
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步