www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to create a UTF16 text file on Windows?

reply lafoldes <lafoldes geemail.kom> writes:
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.
Nov 16 2016
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
Nov 16 2016
prev sibling parent Kagamin <spam here.lot> writes:
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?
Nov 17 2016