ifstream:检查是否成功打开

编程入门 行业动态 更新时间:2024-10-27 10:32:52
本文介绍了ifstream:检查是否成功打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

一位同事只是告诉我这个代码:

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:检查是否成功打开

本文发布于:2023-11-07 15:02:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566764.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ifstream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!