这似乎很奇怪。我有一段代码如下:
List(const char* fn) {
std::ifstream file(fn);
if (!file){
throw std::string("*** Failed to open file ") + std::string(fn) + std::string(" ***");
}
while (file) {
T e;
if (e.load(file)){
list.push_back(*new T(e));
}
}
}
与其他人一起,似乎整个文件都运行得很好。但对我来说,我在机器上陷入无限循环。
OS X - g ++ 4.2
我不知道为什么这里有不同的功能。
这是无限循环:
while (file) {
T e;
if (e.load(file)){
list.push_back(*new T(e));
}
}
答案 0 :(得分:2)
while(file)
将返回true。
你还没有告诉我们T
是什么或load
方法做了什么,所以我们真的不能给你更多的信息。您需要确保load
方法关闭文件描述符,或将其保留为eof
状态或类似的其他内容。