How to create a UTF16 text file on Windows?

Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 16 15:02:25 PST 2016


Dne 16.11.2016 v 23:43 lafoldes via Digitalmars-d-learn napsal(a):
> Hi,
> I'd like to create a UTF16 text file on Windows 7, using 
> std.stdio.File and std.stdio.File.write... functions (so no binary 
> write, no Win32 functions).
>
> I was experimenting with variations of this code…:
>
> import std.stdio;
>
> int main(string[] argv)
> {
>     auto output = File("wide_text.txt", "wt");
>     output.writeln!wstring("A"w);
>     return 0;
> }
>
> …and didn't succeed; the output was [0x41, 0x0d, 0x0a] and not what I 
> dreamed about: [\ufeff, \u0041, \u000d, 0u000a]. (After I looked into 
> the Phobos source code, well, it was not a surprise...)
>
> VS2015 (and its runtime) has a non-standard solution for this; the c++ 
> code below does the trick:
>
> #include <cstdio>
> #include <cwchar>
>
> void main()
> {
>     FILE * output;
>     fopen_s(&output, "test.txt", "wt+,ccs=UTF-16LE");
>     fwprintf(output, L"A\n");
>     fclose(output);
> }
>
> Do you know about anything of similar complexity in D?
> If not, I think it would be useful.
http://dlang.org/phobos/std_stdio.html#.toFile



More information about the Digitalmars-d-learn mailing list