有没有办法检查 istream 是否以二进制模式打开?

发布于 2024-07-07 20:55:12 字数 304 浏览 8 评论 0原文

我使用的 istream 可以是 stringstream、ifstream 或用户定义的流类型,我需要知道在 ifstream 的情况下它是否未以二进制模式打开(这样我可以抛出异常)。 我尝试过以下方法:

if ((_is.flags() & ios::binary) == 0)
    throw exception(...)

但没有抛出任何异常。 在这种情况下测试失败,因为 _is.flags() 返回 0x201 而 ios::binary 是 0x20。 有没有办法确定流是否以文本模式打开?

I'm using an istream which could be stringstream, ifstream or a user-defined stream type and I need to know if, in the case of an ifstream, it was not opened in binary mode (so I can throw an exception). I have tried the following method:

if ((_is.flags() & ios::binary) == 0)
    throw exception(...)

but no exception is ever thrown. The test fails in this case because _is.flags() returns 0x201 and ios::binary is 0x20. Is there a way to find out if a stream was opened in text mode?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳男子 2024-07-14 20:55:12

flags() 返回 ios_base::fmtflags ,它正在格式化标志,而二进制是一个 ios_base::openmode 标志。 我不确定一旦流已经打开是否有办法找到这些。 我在想也许有一个streambuf类的虚拟成员可以提供帮助,但似乎并没有。

flags() returns ios_base::fmtflags which is formatting flags, whereas binary is an ios_base::openmode flag. I'm not sure if there is a way to find these out once the stream is already open. I was thinking that maybe there was a virtual member of the streambuf class that could help, but there doesn't really seem to be.

楠木可依 2024-07-14 20:55:12

不,没有办法测试这一点。

Nope, there is no way to test this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文