为什么seekg无法与getline一起使用? [英] Why seekg does not work with getline?

查看:159
本文介绍了为什么seekg无法与getline一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Seekg does not seem to work, when I reach EOF in myFile.

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

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

So, now I am opening input stream every loop:

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

但是我宁愿寻求第0位.我该如何实现?

But I would rather seek to position 0. How can I achieve that?

推荐答案

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

Make sure you clear the error flags before the call to myFile.seekg():

myFile.clear();

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

After the EOF flag has ben set, you will not be able to extract anything. You have to clear those flags to be able to extract again.

这篇关于为什么seekg无法与getline一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆