Debian x64 PC で実行している C++ プログラムに奇妙な動作があります。
最初にファイルを読み取ってから、別の値を書き込んでからこれらの値を読み取ることができません。私は、stackoverflow に関する質問を含む多くの情報を読み、seekp と seekg の両方を変更する必要があることを (これも実験によって) 知りました。すべてが機能します...ストリームから何かを読み取るまで。読み取り操作の後、ファイルの先頭をシークしてから tellg()、tellp() を呼び出すと、両方とも「-1」が返されます。
テストコード:
void testFstreamSeekp() {
fstream in("file", ios::in | ios::out);
cout << "g: " << in.tellg() << endl;
cout << "p: " << in.tellp() << endl;
in.seekp(0, ios_base::end);
cout << "endp g: " << in.tellg() << endl;
cout << "endp p: " << in.tellp() << endl;
in.seekp(0, ios_base::end);
in.seekg(0, ios_base::end);
cout << "end g: " << in.tellg() << endl;
cout << "end p: " << in.tellp() << endl;
in.seekp(0, ios_base::beg);
in.seekg(0, ios_base::beg);
cout << "beg g: " << in.tellg() << endl;
cout << "beg p: " << in.tellp() << endl;
// Everything is fine until here (that is tellp() == 0, tellg() == 0)
int a, b;
in >> a >> b;
cout << "a: " << a << endl << "b: " << b << endl;
// tellg() == -1, tellp() == -1 ?????????!!!!!!!!!!
cout << "read g: " << in.tellg() << endl;
cout << "read p: " << in.tellp() << endl;
in.seekp(0, ios_base::beg);
in.seekg(0, ios_base::beg);
// tellg() == -1, tellp() == -1 ?????????!!!!!!!!!!
cout << "beg g: " << in.tellg() << endl;
cout << "beg p: " << in.tellp() << endl;
}
何が起こったのか、問題を解決するために何ができるのか誰か教えてもらえますか?