Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if an istream was opened in binary mode?

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?

like image 635
Ferruccio Avatar asked Oct 22 '08 02:10

Ferruccio


2 Answers

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.

like image 159
Greg Rogers Avatar answered Sep 29 '22 07:09

Greg Rogers


Nope, there is no way to test this.

like image 21
C. Broadbent Avatar answered Sep 29 '22 08:09

C. Broadbent