主题:求救,二进制读写类
//main.cpp
#include<iostream.h>
#include<fstream.h>
#include"person.h"
void main()
{
person psn,pp;
ifstream f1;
ofstream f2;
f2.open("personal.dat",ios::binary);
if (!f2)
{
cout<<"\a can't open person.dat";
return;
}
char ch='y';
cout<<"Plese enter students' information:\n";
while (ch=='y')
{
cin>>psn;
f2.write((char *) &psn,sizeof(class person));
cout<<"Continue? y/n ";cin>>ch;
}
f2.close();
f1.open("personal.dat",ios::binary);
if (!f1)
{
cout<<"\a can't open personal.dat\n";
return;
}
while (f1.read((char *) &pp,sizeof(class person)),!f1.eof())
{
cout<<pp<<endl;
}
f1.close();
}
//person.h
#include<string.h>
class data
{
private:
int year,month,day;
public:
data(int y=2010,int m=5,int d=14){year=y;month=m;day=d;}
virtual ~data(){};
friend istream &operator >> (istream &in,data &p);
friend ostream &operator << (ostream &out,data &p);
};
istream &operator >> (istream &in,data &p)
{
cout<<" the year: ";in>>p.year;
cout<<" the month: ";in>>p.month;
cout<<" the day: ";in>>p.day;
return in;
}
ostream &operator << (ostream &out,data &p)
{
out<<p.year<<"."<<p.month<<"."<<p.day;
return out;
}
class person
{
private:
char *name;
char *sex;
data birthday;
public:
person()
{
name=NULL;
sex=NULL;
}
~person()
{
delete []name;
delete []sex;
}
friend istream &operator >> (istream &in,person &p);
friend ostream &operator << (ostream &out,person &p);
};
istream &operator >> (istream &in,person &p)
{
char na[20],se[10];
cout<<"Enter name: ";in>>na;
cout<<" sex: ";in>>se;
cout<<"Enter birthday:\n ";cin>>p.birthday;
p.name=new char[strlen(na)+1];
p.sex=new char[strlen(se)+1];
strcpy(p.name,na);
strcpy(p.sex,se);
return in;
}
ostream &operator << (ostream &out,person &p)
{
out<<p.name<<" "<<p.sex;
cout<<" birthday is "<<p.birthday;
return out;
}
#include<iostream.h>
#include<fstream.h>
#include"person.h"
void main()
{
person psn,pp;
ifstream f1;
ofstream f2;
f2.open("personal.dat",ios::binary);
if (!f2)
{
cout<<"\a can't open person.dat";
return;
}
char ch='y';
cout<<"Plese enter students' information:\n";
while (ch=='y')
{
cin>>psn;
f2.write((char *) &psn,sizeof(class person));
cout<<"Continue? y/n ";cin>>ch;
}
f2.close();
f1.open("personal.dat",ios::binary);
if (!f1)
{
cout<<"\a can't open personal.dat\n";
return;
}
while (f1.read((char *) &pp,sizeof(class person)),!f1.eof())
{
cout<<pp<<endl;
}
f1.close();
}
//person.h
#include<string.h>
class data
{
private:
int year,month,day;
public:
data(int y=2010,int m=5,int d=14){year=y;month=m;day=d;}
virtual ~data(){};
friend istream &operator >> (istream &in,data &p);
friend ostream &operator << (ostream &out,data &p);
};
istream &operator >> (istream &in,data &p)
{
cout<<" the year: ";in>>p.year;
cout<<" the month: ";in>>p.month;
cout<<" the day: ";in>>p.day;
return in;
}
ostream &operator << (ostream &out,data &p)
{
out<<p.year<<"."<<p.month<<"."<<p.day;
return out;
}
class person
{
private:
char *name;
char *sex;
data birthday;
public:
person()
{
name=NULL;
sex=NULL;
}
~person()
{
delete []name;
delete []sex;
}
friend istream &operator >> (istream &in,person &p);
friend ostream &operator << (ostream &out,person &p);
};
istream &operator >> (istream &in,person &p)
{
char na[20],se[10];
cout<<"Enter name: ";in>>na;
cout<<" sex: ";in>>se;
cout<<"Enter birthday:\n ";cin>>p.birthday;
p.name=new char[strlen(na)+1];
p.sex=new char[strlen(se)+1];
strcpy(p.name,na);
strcpy(p.sex,se);
return in;
}
ostream &operator << (ostream &out,person &p)
{
out<<p.name<<" "<<p.sex;
cout<<" birthday is "<<p.birthday;
return out;
}