0
void updatebfile(char filename[MAX])
{
fstream writereadb;
char cont='y';
char filenameb [MAX];
int i=1;
int record;
student s;
strcpy(filenameb,filename);
strcat(filenameb,".dat");
writereadb.open(filenameb,ios::in | ios::out | ios::binary );
cout<<"------------------------------"
<<endl;
cout<<"Begin updating of binary file "
<<filenameb
<<endl
<<endl;
cout<<"Information for student file"
<<endl
<<endl;
while (writereadb.read (reinterpret_cast <char *>(&s), sizeof (s)))
{
cout<<i
<<'\t'
<<s.identity
<<" "
<<s.name
<<endl;
i++;
}
do
{
cout<<endl
<<"Update record: ";
cin>>record;
cout<<endl
<<"Student id: ";
writereadb.seekg ((record - 1) * sizeof(s), ios::beg);//problem is here
writereadb.read (reinterpret_cast <char *>(&s), sizeof (s));//always reading last value
cout<<s.identity
<<endl;
cout<<"Update the name: ";
cin>>s.name;
writereadb.seekp((record-1)*sizeof(student),ios::beg);
writereadb.write (reinterpret_cast <const char *>(&s), sizeof (s));
cout<<"Any more update (y/n) :";
cin>>cont;
}while (cont=='y');
writereadb.close();
}
我有這個簡單的功能,我想更新二進制文件。問題是我似乎無法設置獲取指針,我總是讀取二進制文件中的最後一個值,當我cout s.identity需要幫助調試簡單更新二進制文件功能
非常感謝,我花了2小時試圖調試它 – Computernerd