www.digitalmars.com         C & C++   DMDScript  

c++ - setting stdout to binary?

reply Heinz Saathoff <hsaat bre.ipnet.de> writes:
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
parent reply "Gisle Vanem" <giva users.sourceforge.net> writes:
"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
parent Heinz Saathoff <hsaat bre.ipnet.de> writes:
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