バイナリ モードでファイルの一部を置き換えるときに問題が発生します。何らかの理由で、seekp() 行がファイル ポインターを目的の位置に配置していません。現在、目的の部分を置き換えるのではなく、ファイルの最後に新しい内容を追加しています。
long int pos;
bool found = false;
fstream file(fileName, ios::binary|ios::out|ios::in);
file.read(reinterpret_cast<char *>(&record), sizeof(Person));
while (!file.eof())
{
if (record.getNumber() == number) {
pos=file.tellg();
found = true;
break;
}
// the record object is updated here
file.seekp(pos, ios::beg); //this is not placing the file pointer at the desired place
file.write(reinterpret_cast<const char *>(&record), sizeof(Person));
cout << "Record updated." << endl;
file.close();
私は何か間違ったことをしていますか?
よろしくお願いします。