如何从 fstream 中获取数据,然后再获取?

How can I take data from fstream and then take it again?

我是c++的初学者,所以想问一下,如何从fstream中获取数据 在我已经从中获取所有数据之后?

我做过这样的事

fstream file;
string data, key, key2;
while(file>>data)
{
    key += data;
}

现在我想用同一个文件再做一次但不重新打开它:

while(file>>data)
{
    key2 += data;
}

如何用 fstream 做到这一点?

后退 流:

file.clear();
file.seekg(0);