一位同事只是告诉我这个代码:
A colleague just told me that this code:
std::ifstream stream(filename.c_str()); if (!stream) { throw std::runtime_error(".."); }他说 ifstream 如果打开成功则计算为0。我的代码工作,但我想找到文档,但没有看到它说如何检查打开是否成功。你能指点我吗?
would be wrong. He said ifstream evaluates to 0 if opening is successful. My code works, but I wanted to find the documentation but didn't see where it says how to check if opening was successful. Can you point me to it?
推荐答案operator! std :: ifstream ,因此您可以执行此操作,因此重新启动了rel =nofollow> 。
operator! is overloaded for std::ifstream, so you can do this.
在我看来,这是一个可怕的滥用操作符重载(由标准委员会)。如果你只是做 if(stream.fail()),它会更清楚。
In my opinion, though, this is a horrible abuse of operator overloading (by the standards committee). It's much more explicit what you're checking if you just do if (stream.fail()).
更多推荐
ifstream:检查是否成功打开
发布评论