65,210
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
using namespace std;
class Animal
{
public:
Animal(int age) {itsage=age;}
~Animal() {};
int getage() const {return itsage;}
private:
int itsage;
};
char strone[250];
Animal bearone(10);
Animal beartwo(20);
char ch;
int main()
{
strcpy(strone,"in.txt");//in.txt是你要打开的文件名
ofstream fout(strone,ios::binary); //strong里面没东西,到这里自然会出错误
if(fout)
{
cout <<"true" <<endl;
}
fout.write((char*) &bearone,sizeof(bearone));
fout.close();
ifstream fin(strone,ios::binary);
if(fin)
{
cout <<"yes" <<endl;
}
cout <<strone <<endl;
fin.read((char*) &beartwo,sizeof(beartwo));
fin.close();
cout <<beartwo.getage() <<endl;
return 0;
}