Binary outout to std::cout

M

mortenf

What is the correct portable way of outputting binary data to cout? I
know that I can call _setmode(1, _O_BINARY) but that only works for
MSDOS.

And I *have* tried to Google it, but I have not been able to produce
any working code from what I found. Could anyone please extend the code
below, so that it writes binary data to cout without modifying it.
(<LF> is modified to <CR><LF> under MSDOS)?

#include <iostream>
void main()
{
std::cout.setf(std::ios_base::binary);
std::cout.write("\r\n", 2);
}
 
P

Peter Jansson

What is the correct portable way of outputting binary data to cout? I
know that I can call _setmode(1, _O_BINARY) but that only works for
MSDOS.

And I *have* tried to Google it, but I have not been able to produce
any working code from what I found. Could anyone please extend the code
below, so that it writes binary data to cout without modifying it.
(<LF> is modified to <CR><LF> under MSDOS)?

#include <iostream>
void main()
{
std::cout.setf(std::ios_base::binary);
std::cout.write("\r\n", 2);
}

See F.A.Q. [15.13] at the follow URL:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.13

"This is implementation dependent. Check with your compiler's
documentation."

Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top