retrieving openmode from a stream

N

Noah Roberts

I am writing a class that uses a passed in stream. The writing this
class performs is binary and so I would like to assert that the stream
is a binary stream. Problem is that I can't find any function to
retrieve that information from the stream. Looked in Josuttis, std,
msdn, and google but can't seem to find it. Never wanted to do this
before. Anyone know if and where such functionality resides?
 
S

Steve Pope

Noah Roberts said:
I am writing a class that uses a passed in stream. The writing this
class performs is binary and so I would like to assert that the stream
is a binary stream. Problem is that I can't find any function to
retrieve that information from the stream. Looked in Josuttis, std,
msdn, and google but can't seem to find it. Never wanted to do this
before. Anyone know if and where such functionality resides?

I'm guessing this functionality isn't there. Unix does not
distinguish between binary files and other types of files,
and C++ I/O was written with Unix in mind.

Another way of saying this is that the stream is always binary,
unless you have some OS-specific (and non-portable) function
that can tell you otherwise.

(Somebody correct me if I'm guessing wrong.)

Steve
 
T

tryptik

Another way of saying this is that the stream is always binary,
unless you have some OS-specific (and non-portable) function
that can tell you otherwise.

My understanding and experience seem to indicate that the stream is
never truly binary - by nature, the stream is imposing formatting
information on the the underlying rdbuf. I say this because I have
been using the stream in binary mode and still having to deal with
strange formatting characters getting in there. (To be fair, I believe
I was picking up '\r' characters being inserted by the MS
implimentation of the stream where none exist in UNIX - there is
perhaps a flag to set this, but I never found it.)

To garauntee binary operations in the stream, you can use the get(),
read() and write() methods (and their associated methods - readsome(),
etc), or access the underlying streambuf directly, which is what I have
been doing. If you use the << or >>, you are not accessing the stream
in a binary fashion regardless of openmode.

-J
 
R

Richard Herring

Steve Pope said:
I'm guessing this functionality isn't there. Unix does not
distinguish between binary files and other types of files,
and C++ I/O was written with Unix in mind.

That may have been true for C, but you can't really say that of C++ -
ios_base::binary is part of the standard, so someone somewhere must have
thought it served some purpose.
Another way of saying this is that the stream is always binary,
unless you have some OS-specific (and non-portable) function
that can tell you otherwise.

(Somebody correct me if I'm guessing wrong.)
Well, an fstream itself "knows" whether ios_base::binary flag was passed
to open(), so if my interpretation of the OP's question as "how can I
find out if the stream was opened with the binary flag?" is correct,
this could (in principle) be achieved by the function
fstream::get_openmode().

Unfortunately it doesn't exist :-(
 
B

BobR

Steve Pope wrote in message ...
I'm guessing this functionality isn't there. Unix does not
distinguish between binary files and other types of files,
and C++ I/O was written with Unix in mind.

Another way of saying this is that the stream is always binary,
unless you have some OS-specific (and non-portable) function
that can tell you otherwise.

(Somebody correct me if I'm guessing wrong.)
Steve

I found this in the MinGW(GCC) header, maybe it could explain (I did'nt check
it out).

/* --- from ..../bits/ios_base.h ---
/// Perform input and output in binary mode (as opposed to text mode).
/// This is probably not what you think it is; see
/// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#3 and
/// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#7 for more.
static const openmode binary = openmode(__ios_flags::_S_bin);

--- other headers ---
struct __ios_flags{
static const __int_type _S_bin = 0x04;
};
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top