Can we get rid of non-raw write?

via Digitalmars-d digitalmars-d at puremagic.com
Wed Apr 15 08:17:26 PDT 2015


On Wednesday, 15 April 2015 at 14:47:46 UTC, armando sano wrote:
> Reviving old topic... It is possible to force stdout to write 
> in binary mode on Windows, see 
> https://msdn.microsoft.com/en-us/library/tw4k6df8.aspx
>
> In C, the solution is:
>
> -----------------------------
> #include <stdio.h>
> #include <fcntl.h>
> #include <io.h>
>
> /*...*/
>
> int result = _setmode( _fileno( stdout ), _O_BINARY );
> if ( result == -1 )
> 	perror ("Cannot set stdout to binary mode");
> else
> 	perror ("stdout set to binary mode");
> ------------------------------
>
>
> In Python, the solution is:
>
> ------------------------------
> import platform
> if platform.system() == "Windows":
>     import os, msvcrt
>     msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
> ------------------------------
>
> Since D can interface C, it must be possible to do the same in 
> D? (how, I am not sure)

my humble solution:

void setFileModeBinary(File f)
{
	import std.c.stdlib;
	version(Windows) {
		immutable fd = _fileno(f.getFP);
		f.flush();
		_setmode(fd, _O_BINARY);
		version(DigitalMars) {
			// @@@BUG@@@ 4243
			immutable info = __fhnd_info[fd];
			__fhnd_info[fd] &= ~FHND_TEXT;
		}
	}
}


More information about the Digitalmars-d mailing list