在getline之后返回到文件的开头 [英] Returning to beginning of file after getline

查看:804
本文介绍了在getline之后返回到文件的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经从文件中读取了所有的行

So i've read all the lines from a file thusly

while (getline(ifile,line))
    {
        // logic
    }

其中ifile是ifstream line是一个字符串

Where ifile is an ifstream and line is a string

我的问题是我现在想再次使用getline,似乎无法返回到文件的开头,因为运行

My problem is I now want to use getline over again, and seem to be unable to return to the beginning of the file, as running

cout << getline(ifile,line);

将返回0

尝试使用:

ifile.seekg (0, ios::beg);

无效,似乎没有效果。我如何回到文件的开头?

To no avail, it seems to have no effect. How do I go back to the start of the file?

推荐答案

由于你已经到达文件的结尾, eof 标志将被设置。您需要使用 ifile.clear - 清除尝试寻找:

Since you have reached the end of the file, the eof flag will be set. You need to clear that using ifile.clearthen try seeking:

ifile.clear();
ifile.seekg(0, ios::beg);

这篇关于在getline之后返回到文件的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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