#include<bits/stdc++.h>
using namespace std;
class Dog{
private:
int age;
int weight;
public:
Dog(int a,int b):age(a),weight(b){};//带参数的构造函数;
Dog(){};//不带参数的构造函数
int Age(){
return this->age;
}
int Weight()
{
return this->weight;
}
~Dog(){};//析构函数;
};
int main(){
Dog dog1(10,5);
Dog dog2;
ofstream out;
out.open("dog1.dat",ios::binary|ios::out);
out.write((char*)(&dog1),sizeof(dog1));//写入dog1的信息
out.close();
ifstream in;
in.open("dog1.dat",ios::binary|ios::in);
in.read((char*)(&dog2),sizeof(dog2));//读取dog1文件内容;
in.close();
cout<<dog2.Age()<<" "<<dog2.Weight()<<endl;
return 0;
}
翻译
搜索
复制
标签:weight,read,ios,dog,Dog,write,int,out,dog1 From: https://www.cnblogs.com/djcf/p/17414393.html