dog二进制输入输出-write和read;

#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;
}

posted @ 2023-05-19 11:22  mo寒流xing  阅读(32)  评论(0)    收藏  举报