有没有办法检查一个istream是否以二进制模式打开? [英] Is there a way to check if an istream was opened in binary mode?

查看:313
本文介绍了有没有办法检查一个istream是否以二进制模式打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个istream它可以是stringstream,ifstream或用户定义的流类型,我需要知道if,ifstream的情况下,它不是以二进制模式打开(所以我可以抛出异常)。我尝试了以下方法:

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(...)

但不会抛出任何异常。在这种情况下测试失败,因为_is.flags()返回0x201和ios :: binary是0x20。

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?

推荐答案

flags()返回ios_base::fmtflags (格式化标记),而binary是 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.

这篇关于有没有办法检查一个istream是否以二进制模式打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆