gpt4 book ai didi

c++ - 检查 string::getline 中的 eof

转载 作者:IT老高 更新时间:2023-10-28 12:32:02 29 4
gpt4 key购买 nike

如何使用 std::getline 函数检查文件结尾?如果我使用 eof() 它不会发出 eof 信号,直到我尝试读取超出文件结尾。

最佳答案

C++ 中的规范阅读循环是:

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)
}

关于c++ - 检查 string::getline 中的 eof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2251433/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com