我有一个函数(addShape
)根据它获得的id
从文件中读取整数。它获取id
和流is
作为参数。由于某种原因,在阅读最后一行后,我得到std::ios_base::failure
。为什么这个文件读取代码会产生异常?
while (is >> id)
addShape(id, is, false);
我认为这是从文件中读取最安全的方法。
这是我的初始化流:
fstream is;
int id = 0;
string filename;
char answer = NULL;
// set exceptions
is.exceptions(fstream::failbit | fstream::badbit);
try { is.open(filename); }
catch (ifstream::failure e)
{
clrscr();
cout << "There was an error opening " << filename << endl;
waitForEscape();
is.close();
return;
}
你能告诉我们你是如何初始化'is'流和'addShape'的代码吗?您可能在流对象上启用了异常。 – templatetypedef