2

Possible Duplicate:
How to make cout behave as in binary mode?

I am using C++ to print binary data to the stdout and then I redirect it to a file. (I do not want to write directly to a file). However, when I try to print the value '0A' I am getting '0D 0A'. (this is what I see when I copy the output to HexEdit). From what I can figure out, '0A' is the ascii for 'new line' so cout automatically adds the '0D' which is 'carriage return'. What I want is to print 0A alone. how can I do this?

my code:

unsigned char *buf = new unsigned char[width+1];
for (int x = 0; x < width; x++) 
{
    buf[x] = (unsigned char) src[x];
}
buf[width] = '\0'
cout<<buf; 

I've seen this: Print new line to a text file without carriage return (CR) in windows but it does not seem to help me.

Community
  • 1
  • 1
Aliza
  • 734
  • 1
  • 10
  • 25
  • Why link you mentioned does not help you? – Lol4t0 Jan 22 '12 at 11:13
  • @Lol4t0: the link points to a C question, not a C++ one. – Fred Foo Jan 22 '12 at 11:18
  • This does not really matter, the thing is that TS should reopen `stdout` in binary mode. – Lol4t0 Jan 22 '12 at 11:23
  • 1
    Thanks to all. what I ended up doing was using _setmode to set the mode of stdout to binary. `fd_stdout = fdopen (1,"wb"); _setmode( _fileno( stdout ), _O_BINARY );` and then I used fwrite to write my binary data to stdout `fwrite(reinterpret_cast(buf), width, 1, fd_stdout);` – Aliza Jan 23 '12 at 13:41

2 Answers2

1

Not sure you really can use cout and the << operator without a massive hack. This is because when using << cout treats all data as text data and will perform appropriate transformations as necessary -- including adding a CR character when printing a newline on a Windows platform.

You will need to use the write method of cout, instead of using the insertion operator <<. The write method will not format your data, but is more awkward to use.

http://www.cplusplus.com/reference/iostream/ostream/write/

Dunes
  • 37,291
  • 7
  • 81
  • 97
  • 1
    even when using `write()` the `\n` gets replaced with an end of line sequence. The conversion is either done on the stream buffer level or on systems even by the system call. – Dietmar Kühl Jan 22 '12 at 11:49
  • I did the following: cout.setf(std::ios_base::binary); cout.write((const char*)buf, width); and I still get the carriage return. – Aliza Jan 22 '12 at 12:19
1

You can't control the end of line sequence with any of the standard output streams or files. The only way to deal with this is to freopen() the standard output stdout with an open mode of wb or wb+` to control the underlying formatting, e.g.:

FILE* out = freopen("stdout.txt", "wb", stdout);
std::cout << "hello, world\n";

If you are only using C++ streams you can also replace std::cout's stream buffer with the stream buffer of an appropriately opened std::ofstream, e.g.:

std::ofstream   file("stdout.txt", std::ios_base::out);
std::streambuf* coutbuf = std::cout.rdbuf(file.rdbuf());
std::cout << "hello, world\n";
...
std::cout.rdbuf(coutbuf);

You need to restore the original stream buffer to avoid problems with uses of std::cout during destruction: at the very least the stream gets flushed at some point after main() is exited. If you can accept a memory leak, you can use a dynamically allocated std::filebuf directly.

None of the solutions above actually writes the output to the standard output, they all write to a file. I don't know how to reopen the standard output on a Windows system (on a UNIX system you'd either create a stream buffer using file descriptor 1 but on UNIX there is no need anyway because the \n doesn't get replaced in the first place). In particular, reopening the stream to fit in with output redirection is probably not possible.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380