【发布时间】:2014-05-27 08:03:11
【问题描述】:
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
【问题讨论】:
-
你需要在调用
seekg之前做fin.reset()。