简体   繁体   English

While(file_object)是导致无限循环的原因

[英]While(file_object) is an causing infinite loop

This seems rather strange. 这似乎很奇怪。 I have a snippet of code that is as follows: 我有一段代码如下:

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

With other people, it seems to run through the whole file just fine. 与其他人一起,似乎可以正常运行整个文件。 But for me, I'm stuck in an infinite loop on my machine. 但是对我来说,我陷入了机器上的无限循环。

OS X - g++ 4.2 OS X-g ++ 4.2

I have no clue as to why there is a difference of function here. 我不知道为什么这里有功能上的区别。

Here is the infinite loop: 这是无限循环:

while (file) {
    T e;
    if (e.load(file)){
        list.push_back(*new T(e));
    }
}

while(file) will return true as long as the file is left in a valid state. 只要文件保持有效状态, while(file)就会返回true。

You haven't told us what a T is or what its load method does, so we can't really give you much more info than that. 您没有告诉我们T是什么或它的load方法是什么,因此我们无法真正提供给您更多的信息。 You need to make sure that the load method either closes the file descriptor, or leaves it in eof state or something else like that. 您需要确保load方法要么关闭文件描述符,要么将其保持在eof状态或类似的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM