ifstream fin;
fin.open("c:\\1\\a.txt");
if(fin)
{
string readWord1;
streampos currentPos;
while(fin >> readWord1)
{
currentPos = fin.tellg();
string readWord2;
while(fin >> readWord2)
{
Memo1->Text = Memo1->Text + AnsiString(readWord2.c_str());
Memo1->Text = Memo1->Text + "\n";
}
fin.seekg(currentPos);
streampos c = fin.tellg();
}
}
阅读完readWord1之后的所有单词后,我想将文件指针返回到currentPos。但是在第一步中,streampos c表示指针在-1中,所以seekg()不会将指针移到currentPos
最佳答案
根据文档(或至少一个版本):
如果在调用之前设置了eofbit标志,则该函数将失败(
故障位并返回)。
您需要使用fin.clear()
清除eofbit,然后它将按预期工作。