【问题标题】:Why seekg does not work with getline?为什么 seekg 不适用于 getline?
【发布时间】:2013-03-21 04:16:36
【问题描述】:

当我在 myFile 中到达 EOF 时,Seekg 似乎不起作用。

ifstream myFile("/path/file");
for(int i; i < 10; i++){
    myFile.seekg(0);//reset position in myFile
    while(getline(myFile, line)){
        doSomething
    }
}

所以,现在我每个循环都打开输入流:

for(int i; i < 10; i++){
    ifstream myFile("/path/file");//reset position in myFile
    while(getline(myFile, line)){
        doSomething
    }
}

但我宁愿寻求位置 0。我怎样才能做到这一点?

【问题讨论】:

标签: c++ ifstream getline seekg


【解决方案1】:

确保在调用myFile.seekg()之前清除错误标志:

myFile.clear();

设置 EOF 标志后,您将无法提取任何内容。您必须清除这些标志才能再次提取。

【讨论】:

    猜你喜欢
    • 2013-06-02
    • 2012-01-10
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多