retrieving openmode from a stream

Discussion in 'C++' started by Noah Roberts, Nov 8, 2006.

  1. Noah Roberts

    Noah Roberts Guest

    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?
    Noah Roberts, Nov 8, 2006
    #1
    1. Advertising

  2. Noah Roberts

    Steve Pope Guest

    Noah Roberts <> wrote:

    >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
    Steve Pope, Nov 9, 2006
    #2
    1. Advertising

  3. Noah Roberts

    Guest

    > 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
    , Nov 9, 2006
    #3
  4. In message <eivpj7$d4l$>, Steve Pope
    <> writes
    >Noah Roberts <> wrote:
    >
    >>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.


    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 :-(

    --
    Richard Herring
    Richard Herring, Nov 10, 2006
    #4
  5. Noah Roberts

    BobR Guest

    Steve Pope wrote in message ...
    >Noah Roberts <> wrote:
    >
    >>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


    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;
    };
    */

    --
    Bob R
    POVrookie
    BobR, Nov 10, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens

    ios::openmode

    Marc Schellens, Jul 15, 2003, in forum: C++
    Replies:
    1
    Views:
    1,691
    Dietmar Kuehl
    Jul 16, 2003
  2. Rasmusson, Lars
    Replies:
    1
    Views:
    754
    popov
    Apr 30, 2004
  3. Al-Burak
    Replies:
    3
    Views:
    271
    Dietmar Kuehl
    Dec 28, 2005
  4. Replies:
    9
    Views:
    629
    Alex Buell
    Apr 27, 2006
  5. Alexander Korsunsky

    get stream mode flags from an opened stream

    Alexander Korsunsky, Feb 17, 2007, in forum: C++
    Replies:
    1
    Views:
    452
    John Harrison
    Feb 17, 2007
Loading...

Share This Page