0

我有一个文件,里面有数字。我想阅读某些行(那些尚未阅读但由于我的代码运行方式而不再易于访问的行)

例如..

我有类似的代码

for (c=0; c < 5;c++)
{
in >> tmp;
}

实现时,这会读取第一行的 5 个部分(行的长度都相同)。

我希望能够再次调用同一段代码并能够阅读 second..third.ect

我需要做什么才能完成这项工作

4

1 回答 1

1

假设in是一个输入流 ( istream),您可以使用它的seekg方法来寻回文件的开头。

// read it the first time
for (c=0; c < 5;c++)
{
    in >> tmp;
}

in.seekg(0, in.beg); // seek to the beginning

// read it the second time
for (c=0; c < 5;c++)
{
    in >> tmp;
}

查看该方法的文档seekg

于 2013-04-03T09:05:38.693 回答