Detecting binary verses text file streams

Discussion in 'C++' started by Tron Thomas, Nov 7, 2004.

  1. Tron Thomas

    Tron Thomas Guest

    What does binary mode for an ofstream object do anyway? Despite which
    mode the stream uses, operator << writes numeric value as their ASCII
    representation.

    I read on the Internet that it is possible to change the behavior of
    operator << so it will stream numeric values as their actual values
    when an ofstream is in binary mode. I did not, however, find any
    information on how this can be accomplished. What is involved in
    getting this to work?

    Given that it might be complicated to change the behavior of operator
    << for binary streams, I would like to be able to determine how an
    object should serialize itself to a stream so that numeric values are
    written as their actually value for binary streams and as their ASCII
    representation for text streams. How can this be done?
     
    Tron Thomas, Nov 7, 2004
    #1
    1. Advertising

  2. Tron Thomas

    osmium Guest

    "Tron Thomas" writes:

    > What does binary mode for an ofstream object do anyway? Despite which
    > mode the stream uses, operator << writes numeric value as their ASCII
    > representation.
    >
    > I read on the Internet that it is possible to change the behavior of
    > operator << so it will stream numeric values as their actual values
    > when an ofstream is in binary mode. I did not, however, find any
    > information on how this can be accomplished. What is involved in
    > getting this to work?
    >
    > Given that it might be complicated to change the behavior of operator
    > << for binary streams, I would like to be able to determine how an
    > object should serialize itself to a stream so that numeric values are
    > written as their actually value for binary streams and as their ASCII
    > representation for text streams. How can this be done?


    There is a lot of bad information on the Internet and it looks like you came
    across some of it. << and >> both implicitly specify conversion to or from
    the underlying character code. For binary files you should use read() and
    write(). Does their use cause you problems? (Other than a presumed
    elegance, that is)
     
    osmium, Nov 7, 2004
    #2
    1. Advertising

  3. "Tron Thomas" <> wrote in message
    news:...
    > What does binary mode for an ofstream object do anyway? Despite which
    > mode the stream uses, operator << writes numeric value as their ASCII
    > representation.


    Yes << for for text output, write is for binary output.

    >
    > I read on the Internet that it is possible to change the behavior of
    > operator << so it will stream numeric values as their actual values
    > when an ofstream is in binary mode. I did not, however, find any
    > information on how this can be accomplished. What is involved in
    > getting this to work?


    That is garbage.

    >
    > Given that it might be complicated to change the behavior of operator
    > << for binary streams, I would like to be able to determine how an
    > object should serialize itself to a stream so that numeric values are
    > written as their actually value for binary streams and as their ASCII
    > representation for text streams. How can this be done?


    There are no such things as binary streams and text streams. Streams are
    just streams. File streams can be opened in binary mode or text mode, but
    you can do text output on a stream opened in binary mode, and you can do
    binary output on a stream opened in text mode (not advisable though).

    Text mode, binary mode is commonly misunderstood. In text mode the
    implementation is allowed to make certain transformations of characters read
    and written, a common example is converting \n to \r\n on output on PC
    systems. This makes perfect sense if you are doing text output but it is
    disastrous if you are doing binary output.

    Text output use <<, binary output use write, and if you are planning on
    binary output then open in binary mode so you don't get any character
    transformations.

    john
     
    John Harrison, Nov 7, 2004
    #3
  4. Tron Thomas

    Tron Thomas Guest

    "osmium" <> wrote in message news:<>...
    > There is a lot of bad information on the Internet and it looks like you came
    > across some of it. << and >> both implicitly specify conversion to or from
    > the underlying character code. For binary files you should use read() and
    > write(). Does their use cause you problems? (Other than a presumed
    > elegance, that is)


    The user of read and write do not cause any problems. I justed wanted
    to know whether I wanted to use operator << or the write method
    depending on whether the file was meant to be written in binary format
    or text format.

    I was hoping the mode of the stream would help me make this
    determination. It appears that there isn't a lot of difference
    between a stream in binary mode and a stream in text mode. This looks
    like knowing which method of writing to the stream is something I
    would have to track outside of the stream.
     
    Tron Thomas, Nov 8, 2004
    #4
    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. wh
    Replies:
    2
    Views:
    601
    Cowboy \(Gregory A. Beamer\)
    Jan 16, 2004
  2. Leon
    Replies:
    4
    Views:
    400
  3. Hoffmania
    Replies:
    1
    Views:
    941
    =?Utf-8?B?Y2xpY2tvbg==?=
    Mar 29, 2006
  4. , India

    text and binary streams

    , India, Aug 23, 2008, in forum: C Programming
    Replies:
    4
    Views:
    449
    Peter Nilsson
    Aug 25, 2008
  5. Bill Cunningham

    streams binary and text

    Bill Cunningham, Aug 29, 2012, in forum: C Programming
    Replies:
    4
    Views:
    394
    Ben Bacarisse
    Aug 29, 2012
Loading...

Share This Page