0

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.

Unhandled exception Microsoft C++ exception: 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) }

4

1 回答 1

2

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

file.exceptions(std::ios_base::goodbit);
于 2010-02-27T09:24:19.750 回答