setmode again

Brian Myers bmyers at harryanddavid.com
Fri Aug 8 11:34:17 PDT 2008


Ugh,

By the rush to respond I'm getting so far, I'm guessing nobody does these kind of programs these days. Can't say I'm surprised, but I'm stuck working with legacy data.

Anyway, for everyone's reference, the following python code works perfectly:

import os, msvcrt
import sys

msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
print "test line1\r\ntestline2"

Brian Myers Wrote:

> Hi all,
> 
> Seems this gets revisited every so often on the forums, so I guess it's time to visit it again...
> 
> I need to set stdout to binary mode in Windows. I've tried the following:
> 
> import std.string;
> import std.stdio;
> import std.c.windows.windows;
> 
> extern (C)
> alias int function(int,int) setmode_f;
> 
> void main(char[][] args)
> {
>     int O_BINARY = 0x8000;
>     setmode_f f;
>     HMODULE m = cast(HMODULE) LoadLibraryA(toStringz("msvcrt.dll"));
>     f = cast(setmode_f) GetProcAddress(m, toStringz("_setmode"));
>     f(fileno(stdout), O_BINARY);
> 
>     writef("test line1\r\ntestline2");
> }
> 
> and
> 
> import std.string;
> import std.stdio;
> 
> extern (C)
> alias int function(int,int) setmode_f;
> 
> extern ( C ) int setmode(int,int);
> 
> void main(char[][] args)
> {
>     int O_BINARY = 0x8000;
> 
>     if (setmode(fileno(stdout) ,O_BINARY) < 0)
>         writefln("setmode failed.");
>     writef("test line1\r\ntest line2");
> }
> 
> No luck. Both compile and run without error, but Windows is still doing LF->CRLF translation.
> 
> I know I need a version statement for a cross platform compile, but this is just example code.
> 
> Brian
> 




More information about the Digitalmars-d mailing list