class Student
{
public:
Student *prev;
char S_Name[15];
char F_Name[15];
int Reg_Num;
char Section;
char MAoI[15];
float CGPA;
Student *next;
}
我有上面的类,我想在退出程序时将数据写入链接列表的二进制文件,并在程序运行时再次读回并形成链接列表。我已经尝试过,但几次尝试都失败了!
输入数据的代码`Student * Add_Entry() { Student *temp=new Student();
char s_Name[15];
char f_Name[15];
int reg_Num;
char section;
char mAoI[15];
float cGPA;
cout <<"**********************Menu***************************\n ";
cout<<"\nEnter the Studets name \"";
cin>>s_Name;
cout<<"\nEnter Father`s name \"";
cin>>f_Name;
cout<<"\nEnter the Registration Number \"";
cin>>reg_Num;
cout<<"\nEnter the Section \"";
cin>>section;
cout<<"\nEnter the Major Area of Interest \"";
cin>>mAoI;
cout<<"\nEnter the Current CGPA \"";
cin>>cGPA;
strcpy_s(temp->S_Name,s_Name);
strcpy_s(temp->F_Name,f_Name);
temp->Reg_Num=reg_Num;
temp->Section=section;
strcpy_s(temp->MAoI,mAoI);
temp->CGPA=cGPA;
temp->next=NULL;
temp->prev=NULL;
return temp;
//temp=Create_node(s_Name,f_Name,reg_Num,section,mAoI,cGPA);
}`
要从文件中读取,我使用 ` char *buffer;
ifstream reader;
reader.open("student.bin",ios::in | ios::binary);
if(reader.is_open)
{
do
{
reader.read(buffer,ch);
if(Header==NULL)
{
Header=(Student)*buffer;
temporary=Header;
}
else
{
temporary->next=(Student)*buffer;
temporary=temporary->next;
}
}while(buffer!=NULL);
}
`
写我使用`temporary=Header; //stream writer的备份条目;writer.open("student.bin",ios::out | ios::binary);
while(temporary!=NULL)
{
writer.write((char)* temporary,sizeof(temporary));
temporary=temporary->next;
}
writer.close();
`