Thread overview | |||||
---|---|---|---|---|---|
|
March 08, 2004 setting stdout to binary? | ||||
---|---|---|---|---|
| ||||
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 Re: setting stdout to binary? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Heinz Saathoff | "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 Re: setting stdout to binary? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gisle Vanem | 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
|
Copyright © 1999-2021 by the D Language Foundation