stdout in binary mode

bearophile bearophileHUGS at lycos.com
Thu Jul 4 08:02:24 PDT 2013


Mike Parker:

>> setmode() should be in unistd.h, but I can't import 
>> core.stdc.unistd (and I don't find it in 
>> std.c.windows.windows).
>
> core.sys.posix.unistd

I have tried this on Windows32, and it doesn't find much:

import core.sys.posix.unistd: setmode;
import core.sys.posix.stdio: fileno;
import core.stdc.stdio: stdout;
void main() {
     setmode(fileno(stdout), O_BINARY);
}


On StackOverflow I have seen in C on Windows you use something 
like:
http://stackoverflow.com/questions/9554252/c-c-is-it-possible-to-pass-binary-data-through-the-console


#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main() {
    int result = _setmode(_fileno(stdin), _O_BINARY);
    return 0;
}


Trying another way I have seen this:
http://bytes.com/topic/c/answers/213649-how-write-binary-data-stdout

This is supposed to be valid code in C99 (despite binary mode is 
not guaranteed to work on all systems), this gives no errors in D 
but gives no output to me:


import core.stdc.stdio: stdout, freopen;
import std.stdio: write;
void main() {
     auto f = freopen(null, "wb", stdout);
     assert(f);
     write("hello\n\n");
}

Maybe it's a good idea to put something portable to do that in 
Phobos... Or maybe core.stdc.stdio.freopen is just not C99 
compliant and should be fixed.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list