Thread overview
How to create a UTF16 text file on Windows?
Nov 16, 2016
lafoldes
Nov 16, 2016
Daniel Kozak
Nov 17, 2016
Kagamin
November 16, 2016
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.
















November 17, 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

November 17, 2016
On Wednesday, 16 November 2016 at 22:43:55 UTC, lafoldes wrote:
> 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;
> }

What C runtime do you use?