www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - setting stdout to binary?

↑ ↓ ← 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
↑ ↓ "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
↑ ↓ → 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