问题描述
我使用一个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()返回(格式化标记),而binary是标志。我不知道有没有办法找到这些出来一旦流已经打开。我在想,也许有一个虚拟成员的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是否以二进制模式打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!