Thread overview
setting stdout to binary?
Mar 08, 2004
Heinz Saathoff
Mar 08, 2004
Gisle Vanem
Mar 09, 2004
Heinz Saathoff
March 08, 2004
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
March 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
March 09, 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