Is there a way to set stdout to binary mode?

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

I tried the function:

freopen(NULL,"wb",stdout)

but my program crashes when I do this.

+8
c ++
source share
2 answers

I tried the code 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 

On Linux, you cannot do this because on this platform, binary and text mode are the same thing.

+10
source share

The simple answer is no. The mode is determined when the iostream object is constructed and cannot be changed later. Some implementations may provide facilities for this later, but this is not standardized. In some implementations, executing freopen on stdout may change the mode, although I believe that this is formal, it is forbidden in C ++. (This implementation is defined in C.) And, apparently, it does not work on your implementation.

It is best to find out what your system calls the console device ( "/dev/tty" under Unix; "CONS" , I think, on Windows), open it in the desired mode and output it to it.

0
source share

All Articles