Tricky cout with 0x0A value

Discussion in 'C++' started by Jean-Gael GRICOURT, Feb 1, 2004.

  1. I am trying to use ostringstream in binary mode. Why the value (0x0A)
    is automatically replaced by (0x0D,0x0A) in the output ? Is there a
    workaround to this issue ?

    -------- my snippet code -------

    #include <iostream>
    #include <sstream>
    using namespace std;

    int main(int argc, char **argv) {
    char buf[512];
    ostringstream oss (ostringstream::eek:ut | ostringstream::binary);

    buf[0] = 0x0A;
    oss.write(buf, 1);
    cout << oss.str();
    return 0;
    }

    -------- output -------
    a.exe > data.txt
    A binary dump of data.txt gives: 0D 0A and not 0A as expected.
     
    Jean-Gael GRICOURT, Feb 1, 2004
    #1
    1. Advertising

  2. On Sun, 01 Feb 2004 12:56:02 -0800, Jean-Gael GRICOURT wrote:

    > I am trying to use ostringstream in binary mode. Why the value (0x0A)
    > is automatically replaced by (0x0D,0x0A) in the output ? Is there a
    > workaround to this issue ?
    >
    > -------- my snippet code -------
    >
    > #include <iostream>
    > #include <sstream>
    > using namespace std;
    >
    > int main(int argc, char **argv) {
    > char buf[512];
    > ostringstream oss (ostringstream::eek:ut | ostringstream::binary);
    >
    > buf[0] = 0x0A;
    > oss.write(buf, 1);
    > cout << oss.str();
    > return 0;
    > }
    >
    > -------- output -------
    > a.exe > data.txt
    > A binary dump of data.txt gives: 0D 0A and not 0A as expected.


    The stringstream is binary, but cout is not. So you output the LF to
    cout which translates it to CRLF. BTW, why not us '\n' instead of 0x0A?

    HTH,
    M4
     
    Martijn Lievaart, Feb 1, 2004
    #2
    1. Advertising

  3. Jean-Gael GRICOURT

    David Harmon Guest

    On 1 Feb 2004 12:56:02 -0800 in comp.lang.c++,
    (Jean-Gael GRICOURT) was alleged to have written:
    > oss.write(buf, 1);
    > cout << oss.str();


    >-------- output -------
    >a.exe > data.txt
    >A binary dump of data.txt gives: 0D 0A and not 0A as expected.


    The binary ostringstream is probably fine, but cout is text mode (and
    there is no standard portable way to change it to binary) and can be
    expected to mangle 0x0A just as you observed. You probably need to
    create a binary ofstream to do your output.
     
    David Harmon, Feb 1, 2004
    #3
  4. Jean-Gael GRICOURT

    David Harmon Guest

    On Sun, 01 Feb 2004 22:08:11 +0100 in comp.lang.c++, Martijn Lievaart
    <> was alleged to have written:
    >BTW, why not us '\n' instead of 0x0A?


    '\n' is the portable way to represent whatever your platform uses for a
    newline separator (in text files.)

    0x0A is a way to represent that you may expect to find in a binary file
    regardless of they way your platform handles text lines.

    In case they might not be the same, the latter is the one he wants.
     
    David Harmon, Feb 1, 2004
    #4
  5. Jean-Gael GRICOURT

    DarkSpy Guest

    at 1 Feb 2004 12:56:02 -0800 , (Jean-Gael GRICOURT)
    wrote:

    >-------- output -------
    >a.exe > data.txt
    >A binary dump of data.txt gives: 0D 0A and not 0A as expected.

    this is windows pipeline's bug, not program's problem :)
    if u create a file on unix system, that is ok.

    [comp.lang.c++]
    [comp.lang.c++.moderated]
    DarkSpy, A C++ MadDog. :)
     
    DarkSpy, Feb 2, 2004
    #5
  6. On Sun, 01 Feb 2004 21:29:47 +0000, David Harmon wrote:

    > On Sun, 01 Feb 2004 22:08:11 +0100 in comp.lang.c++, Martijn Lievaart
    > <> was alleged to have written:
    >>BTW, why not us '\n' instead of 0x0A?

    >
    > '\n' is the portable way to represent whatever your platform uses for a
    > newline separator (in text files.)
    >
    > 0x0A is a way to represent that you may expect to find in a binary file
    > regardless of they way your platform handles text lines.
    >
    > In case they might not be the same, the latter is the one he wants.


    Yes, absolutely. Forget I ever said it, it is most probably just the
    problem condensed to the trouble value.

    M4
     
    Martijn Lievaart, Feb 2, 2004
    #6
  7. Thank you all for your prompt replies. I finally found out what was
    wrong. "Cout" involves an OS dependent IO operation. On the Win32
    plateform it has the side effect I've described in my previous post.
    So I added the necessary code to change this default behaviour to make
    sure that the output will be binary:

    #ifdef _WIN32
    #include <io.h>
    #include <fcntl.h>
    #endif

    [...]

    #ifdef _WIN32
    _setmode(_fileno(stdin), _O_BINARY);
    _setmode(_fileno(stdout), _O_BINARY);
    #endif

    end of the story ...
     
    Jean-Gael GRICOURT, Feb 2, 2004
    #7
    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. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,653
  2. cesare
    Replies:
    1
    Views:
    338
    Dennis Lee Bieber
    Mar 31, 2006
  3. Replies:
    15
    Views:
    1,108
    Keith Thompson
    Mar 14, 2006
  4. Replies:
    9
    Views:
    539
    CBFalconer
    Apr 25, 2006
  5. Vivienne
    Replies:
    3
    Views:
    454
    Vivienne
    Jan 15, 2007
Loading...

Share This Page