gpt4 book ai didi

c++ - while(file_object) 是一个导致无限循环的原因

转载 作者:行者123 更新时间:2023-11-28 02:36:20 26 4
gpt4 key购买 nike

这看起来很奇怪。我有一段代码如下:

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

对于其他人来说,它似乎贯穿整个文件就好了。但对我来说,我陷入了机器上的无限循环。

操作系统 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 状态或其他类似状态。

关于c++ - while(file_object) 是一个导致无限循环的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27302810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com