Thread overview
stdout in binary mode
Jul 04, 2013
bearophile
Jul 04, 2013
bearophile
Jul 04, 2013
Mike Parker
Jul 04, 2013
bearophile
July 04, 2013
How do you open stdout in binary mode with D/Phobos?

In C++ you use something like:

setmode(fileno(stdout), O_BINARY);

(I don't even know where to find O_BINARY in core.stdc).

Bye and thank you,
bearophile
July 04, 2013
On Wed, 03 Jul 2013 20:20:17 -0400, bearophile <bearophileHUGS@lycos.com> wrote:

> How do you open stdout in binary mode with D/Phobos?

Same way you would do it in C.  D uses C's FILE * as it's implementation.

> In C++ you use something like:
>
> setmode(fileno(stdout), O_BINARY);
>
> (I don't even know where to find O_BINARY in core.stdc).

It may not be present, but it's just a number.  Look it up.

-Steve
July 04, 2013
Steven Schveighoffer:

>> In C++ you use something like:
>>
>> setmode(fileno(stdout), O_BINARY);
>>
>> (I don't even know where to find O_BINARY in core.stdc).
>
> It may not be present, but it's just a number.  Look it up.

Adding a hardcoded magic number in my code isn't very good.

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).

fileno() should be in std.stdio or core.stdc.stdio, but I can't find it.


In Python I use:

import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

Bye,
bearophile
July 04, 2013
On Thursday, 4 July 2013 at 12:28:07 UTC, bearophile wrote:
> Steven Schveighoffer:
>

>
> 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

setmode isn't actually standard C nor of the Posix standard. It's a BSD thing. Generally, you have to link with libbsd to use it on Posix systems. You won't find it in the standard unistd.h[1].

[1] http://manpages.ubuntu.com/manpages/gutsy/man7/unistd.h.7posix.html

>
> fileno() should be in std.stdio or core.stdc.stdio, but I can't find it.

core.sys.posix.stdio
July 04, 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