而(file_object)是一个导致无限循环
While(file_object) is an causing infinite loop
这似乎很奇怪。我有一段代码,如下所示:
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));
}
}
只要
文件保持有效状态,while(file)
就会返回 true。
您还没有告诉我们什么是T
或其load
方法的作用,因此我们无法为您提供比这更多的信息。您需要确保 load
方法要么关闭文件描述符,要么将其保留eof
状态或其他类似状态。
相关文章:
- 为什么这个递归函数会创建一个无限循环?
- 如何找出为什么我的程序一直有一个无限循环?
- 为什么我有一个无限循环,我如何以不同的方式做到这一点
- WinUsb_WritePipe进入了一个无限循环
- 为什么我会得到一个无限循环(因子)?
- 为什么我总是得到一个无限循环
- 这是一个无限循环吗?程序提前完成
- 为什么它是一个无限循环?
- 为什么我的程序在输入字符时有一个无限循环
- 为什么这段代码最终会进入一个无限循环,读取std::cin
- 如何使其成为一个无限循环
- Tcl_CreateInterp怎么会变成一个无限循环呢
- C++,得到一个无限循环
- C++ 使用 cin.fail(), cin.clear() ---我有一个无限循环,不知道为什么
- 这个递归函数(回文)变成了一个无限循环
- 当对象被卡在一个无限循环中而被破坏时会发生什么
- 代码在小输入时运行良好,但在大输入时就像一个无限循环
- 从文件中读取信息后,会发生一个无限循环
- 数独求解矩阵,而语句给出了一个无限循环
- 为什么这是c++中的一个无限循环