2010-02-27 159 views
0

我正在使用其中一个相关问题中使用的表单。唯一的问题是,我一直在文件的最后。C++ EOF Getline错误

该文件是一个fstream和str是一个字符串。

未处理的异常 微软C++异常:性病::的ios_base ::失败

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

回答

2

如果您收到std::ios_base::failure抛出异常它极有可能是你引起的(或者你正在使用一些代码)将它们打开以供您的文件使用。它们应该默认关闭。只是为了测试,你可以尝试在while循环之前立即关闭它们,但你可能需要调查什么是打开它们。

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

谢谢,没有注意到,一些打开文件例外示例代码了, raw_data.exceptions(ifstream的:: failbit | ifstream的:: badbit); – Roo 2010-02-27 09:38:19