15

Is there way to set stdout to binary mode? In which mode is stdout without any operations, from my debugging issues I assume that it is in text mode, is it true?

I tried function:

freopen(NULL,"wb",stdout)

but my program is crashes when I'm doing it.

CppMonster
  • 1,216
  • 4
  • 18
  • 35
  • If you need 'binary mode' omit formatted output - stick to unformatted output –  Apr 16 '14 at 11:19
  • @DieterLücking Unformatted output has nothing to do with whether the output is binary or text. – James Kanze Apr 16 '14 at 11:38
  • 4
    This is useful on Windows, because if you do fwrite("a\nb", 1, 3, stdout) on Windows, you will end up writing "a\r\nb" (notice the addition of the \r). This makes it impossible to use stdout as a channel for emitting arbitrary binary data, which can be useful. – Ben Harper Jun 24 '15 at 06:52
  • Possible duplicate of [What is the simplest way to write to stdout in binary mode?](http://stackoverflow.com/questions/16888339/what-is-the-simplest-way-to-write-to-stdout-in-binary-mode) – wonder.mice Oct 15 '15 at 21:13

2 Answers2

19

I tried code presented below to set stdin and stdout to binary mode (on Windows):

#ifdef _WIN32
  #include <io.h>
  #include <fcntl.h>
#endif
...
#ifdef _WIN32
  setmode(fileno(stdout),O_BINARY);
  setmode(fileno(stdin),O_BINARY);
#endif

Under Linux you can't do it, because on this platform binary and text mode is the same thing.

CppMonster
  • 1,216
  • 4
  • 18
  • 35
  • 1
    This works. You can test it by doing fwrite("a\nb", 1, 3, stdout); If the resulting file is 3 bytes, then stdout is binary. Without the above setmode() calls, you'll end up with a 4 byte file. – Ben Harper Jun 24 '15 at 06:45
0

The simple answer is no. The mode is determined when the iostream object is constructed, and cannot be changed later. Some implementations may provide a means of doing it later, but this isn't standardized. On some implementations, doing an freopen on stdout might change the mode, although I think that formally, this is forbidden in C++. (It is implementation defined in C.) And apparently, it doesn't work on your implementation.

You're best bet is to find out how your system names the console device ("/dev/tty" under Unix; "CONS", I think, under Windows), open it in the desired mode, and output to it.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • 2
    Just `CON` rather than `CONS` but this isn't likely to help - in scenarios where you need binary output, it is unlikely that said output is meant to be going to the console. You probably won't even *have* a console. (The most typical scenario is a CGI script that is, e.g., dynamically generating a JPEG for a web server.) – Harry Johnston Jan 07 '17 at 23:50
  • output might have been redirected to a file or a process. it is normally open when the process starts so "open it in the desired mode" makes little sense to me. And if I was to open("/dev/tty", "wb"), that would certainly not be the same target as /dev/stdout in the general case. – PypeBros Apr 20 '18 at 13:10