Is there way to set stdout to binary mode?

前端 未结 2 1508
庸人自扰
庸人自扰 2020-12-30 08:02

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 t

相关标签:
2条回答
  • 2020-12-30 08:35

    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.

    0 讨论(0)
  • 2020-12-30 08:55

    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.

    0 讨论(0)
提交回复
热议问题