该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
//#include
#include
#include
using namespace std;
class A
{
private:
int a;
std::string n;
public:
A(){}
A(const int a1,const std::string n1){a=a1;n=n1;}
A(A &x){a=x.a;n=x.n;}
void print(){std::cout<
};int main()
{
//std::vector
//std::vector
A a1(1,"a");
A a3=a1;
//读取文件m.dat
ifstream fin;
fin.open("m.dat",ios_base::in|ios_base::binary);
if(fin.is_open())
{
cout << "文件打开成功" ;
while(fin.read((char *) &a3,sizeof a3))
{
a3.print();//输出到屏幕
}
fin.close();
}
//输出到文件m.dat
ofstream fout("m.dat",ios_base::out|ios_base::app|ios_base::binary);
if(!fout.is_open())
{
cerr << "不能输出文件" ;
exit(EXIT_FAILURE);
}
a3=a1;
fout.write((char *) &a3,sizeof a3);
fout.close();
//再次读取文件
fin.clear();
fin.open("m.dat",ios_base::in|ios_base::binary);
if(fin.is_open())
{
while(fin.read((char *) &a3,sizeof a3))
{
a3.print();
}
fin.close();
}
return 0;
}