简体   繁体   English

C ++ EOF Getline错误

[英]C++ EOF Getline Error

I was using the form used in one of the related questions. 我正在使用其中一个相关问题中使用的表格。 Only problem is that i keep getting right at the end of the file. 唯一的问题是,我一直在文件结尾处保持正确。

The file is an fstream and the str is a string. 该文件是fstream,str是字符串。

Unhandled exception Microsoft C++ exception: std::ios_base::failure 未处理的异常Microsoft C ++异常:std :: ios_base :: failure

while (getline(file, 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) }

If you are getting std::ios_base::failure exceptions thrown it is most likely caused by you (or some code that you are using) turning them on for your file. 如果您收到std::ios_base::failure异常,则很可能是由您(或您正在使用的某些代码)为文件打开它们引起的。 They should be off by default. 默认情况下,它们应该处于关闭状态。 Just to test, you can try turning them off immediately before the while loop, but you probably need to investigate what is turning them on. 为了进行测试,您可以尝试在while循环之前立即关闭它们,但是您可能需要研究打开它们的原因。

file.exceptions(std::ios_base::goodbit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM