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++;
}
writereadb.clear();
do
{
cout<<endl
<<"Update record: ";
cin>>record;
cout<<endl
<<"Student id: ";
writereadb.seekg ( (record - 1) * sizeof(s), ios::beg);
writereadb.read (reinterpret_cast <char *>(&s), sizeof (s));
cout<<s.identity
<<endl;
cout<<"Update the name: ";
cin.getline(s.name,50); //My programme does not stop at this line to read in an input for some reason
//cin.clear();
//cin.ignore(200,'\n');
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();
}
私のプログラムは、プログラムの最後でcin.getline(s.name,50)
. プログラムは入力の読み取りを停止せず、実行を継続します。
cin.clear(); cin.ignore;
の後に入れてみましたが、動作しますが、バイナリファイルに保存されcin.getline(s.name,50)
ません。s.name