void createtask(char filename[MAX])
{
char filenameb[MAX];
strcpy(filenameb,filename);
strcat(filenameb,".dat");
fstream writeb;
fstream readb;
writeb.open(filenameb,ios::out | ios::binary);
task t;
int nooftask;
cout<<"-----------------------------------"
<<endl
<<endl
<<"Creation of task"
<<endl
<<endl
<<"How many task ?: ";
cin>>nooftask;
cin.clear();
cin.ignore(200,'\n');
for(int i=1;i<=nooftask;i++)
{
t.task_no=i;
cout<<endl
<<"Information for task "
<<i
<<endl
<<endl;
cout<<"Title for assessment: ";
cin.getline(t.task_title,200);
cout<<"Weight (%): ";
cin>>t.task_weight;
cout<<"Full mark upon: ";
cin>>t.task_fullmark;
cin.clear();
cin.ignore(200,'\n');
cout<<"Description: ";
cin.getline(t.task_descrip,200);
writeb.write(reinterpret_cast <const char *>(&t), sizeof (t));
cout<<t.task_descrip
<<endl
<<t.task_fullmark
<<endl
<<t.task_no;
}
writeb.clear();
writeb.close();
cout<<endl
<<endl
<<"The creation of task file is completed"
<<endl
<<endl
<<"The summary of creation"
<<endl
<<endl;
//summarytask(readb,filenameb);
//start
cout<<"Summary of task"
<<endl
<<endl;
readb.open(filenameb,ios::binary | ios::in);
while (readb.read(reinterpret_cast < char *>(&t), sizeof (t)) ); // problem is here
{
cout<<endl
<<endl
<<t.task_no // programme only output the last record in binary file
<<endl
<<t.task_title;//
}
readb.close();
//end
}
一切正常,除了我似乎无法将二进制文件中的所有记录输出到控制台窗口。由于某种原因,我只能输出二进制文件中的最后一条记录,谢谢