c++ - setting stdout to binary?
- Heinz Saathoff (18/18) Mar 08 2004 Hello,
- Gisle Vanem (6/25) Mar 08 2004 Try freopen("con", "wb", stdout). Or
- Heinz Saathoff (9/12) Mar 09 2004 freopen seems to use another new file handle. Using redirection or
Hello, I tried to set stdout to binary mode with setmode but it doesn't work. Compiling this code #include <stdio.h> #include <io.h> #include <fcntl.h> int main() { if(_setmode(fileno(stdout), _O_BINARY) < 0) fprintf(stderr, "Setmode failed\n"); printf("A\nB\nC\n"); return 0; } does not set the stdout to binary and doesn't print the failure message to stdout when compiled as W32 console or X32 application. Compiling this as a DOS-App the error message is printed. Is this a _setmode bug? - Heinz
Mar 08 2004
"Heinz Saathoff" wrote:I tried to set stdout to binary mode with setmode but it doesn't work. Compiling this code #include <stdio.h> #include <io.h> #include <fcntl.h> int main() { if(_setmode(fileno(stdout), _O_BINARY) < 0) fprintf(stderr, "Setmode failed\n"); printf("A\nB\nC\n"); return 0; } does not set the stdout to binary and doesn't print the failure message to stdout when compiled as W32 console or X32 application. Compiling this as a DOS-App the error message is printed. Is this a _setmode bug?Try freopen("con", "wb", stdout). Or stdout = fdopen(FILENO_STDOUT,"wb"); Problem with _setmode() I think is that you must tell stdio functions (printf etc.) to use binary mode. _setmode() is too low-level. --gv
Mar 08 2004
Hello, Gisle Vanem wrote...Try freopen("con", "wb", stdout). Or stdout = fdopen(FILENO_STDOUT,"wb");freopen seems to use another new file handle. Using redirection or piping doesn't work with this method. The text is alwas displayed in the console window. The second method with fdopen failes (return NULL instead of a file pointer). Seems to be something special with stdout. - Heinz
Mar 09 2004