在string :: getline中检查eof [英] checking for eof in string::getline

查看:213
本文介绍了在string :: getline中检查eof的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用string :: getline函数检查文件结束?不建议使用.eof(),因为它不会指示eof直到我尝试读取超出eof。

how do I check for end-of-file using the string::getline function? Coz, using .eof() is not recommended as it wont signal eof until I attempt to read beyond eof.

推荐答案

C ++中的读取循环是:

The canonical reading loop in C++ is:

while (getline(cin, str)) {

}

if (cin.bad()) {
    // IO error
} else if (!cin.eof()) {
    // format error (not possible with getline but possible with operator>>)
} else {
    // format error (not possible with getline but possible with operator>>)
    // or end of file (can't make the difference)
}

这篇关于在string :: getline中检查eof的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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