65,211
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<fstream>
using namespace std;
class student
{
public:
void set(int n,char p[],float s)
{
number=n;
strcpy(name,p);
//name=p;
score=s;
}
void display()
{
cout<<"name:"<<name<<endl;
cout<<"number:"<<number<<endl;
cout<<"score:"<<score<<endl;
}
private:
int number;
char name[10];
float score;
};
void main()
{
student list[2];
student List[2];
fstream myfile;
char p[10];
int num=0;
float scor=0;
cout<<"please input the students' number name and score:";
for(int i=0;i<2;i++)
{
cin>>num>>p>>scor;
list[i].set(num,p,scor);
}
myfile.open("d:\\student.dat",ios::out|ios::binary);
if(!myfile.is_open())
{
cerr<<"something wrong has happend during the reading!"<<endl;
exit(0);
}
myfile.write((char *)List,sizeof(list));
myfile.write((char *)List,sizeof(list));
myfile.close();
cout<<"读取信息后为:"<<endl;
list[0].display();
list[1].display();
myfile.open("d:\\student.dat",ios::in|ios::binary);
myfile.read((char *)List,sizeof(list));
myfile.read((char *)List,sizeof(list));
myfile.close();
}
for(int i=0;i<2;i++)
{
cin>>num>>p>>scor;//score ,这里变量写错
list[i].set(num,p,scor);//score
}