0

私が達成したいことを説明する最良の方法は、次のスレッドのソリューションを組み合わせることです: Windows Unicode C++ Stream Output Failure (but with utf-16)

桁区切り記号としてスペースを使用して数字を印刷するにはどうすればよいですか? (ただし、セパレータとしてドットを使用)

次の解決策の試行は、出力に影響しませんでした。

//include appropriate headers

struct DotSepUTF16 : public codecvt_utf16<wchar_t, 0x10ffffUL, little_endian>
{
    wchar_t do_thousands_sep()const{ return L'.'; }
    string do_grouping(){ return "\3"; }
};
int main()
{
unsigned long num=135412565UL;
locale dot_separated(locale(), new DotSepUTF16);
wofstream fout(L"C:\\Work\\report.htm");
fout.imbue(dot_separated);
const unsigned short BOM= 0xFEFF;
fout.write((wchar_t*)&BOM, 1);
fout<<num;//still no separation
}

//second attempt

struct DotSep : public numpunct < wchar_t >
{
     wchar_t do_thousands_sep()const{ return L'.'; }
     string do_grouping(){ return "\3"; }
};

int main(void)
{  
unsigned long num=135412565UL;
locale utf16(locale(), new codecvt_utf16<wchar_t, 0x10ffffUL, little_endian>());
locale dot_separated(locale(), new DotSep);
locale mylocale(utf16, dot_separated, locale::numeric);//try to combine the locales for utf16 and dot separation
wofstream fout(L"C:\\Work\\report.htm");
fout.imbue(mylocale);
const unsigned short BOM= 0xFEFF;
fout.write((wchar_t*)&BOM, 1);
fout<<num; //again no dots
}

また、MVS/windows はすでにワイド文字列に UTF16 を使用しているため、なぜ utf16 への変換が必要なのか疑問に思います。私が見る限り、これは CPU リソースの浪費です。余分な不要な変換なしでファイルを書き込むことはできませんか? バイナリモードでの書き込みは可能ですが、出力ストリームが提供するすべての便利さを失うため、ばかげています。

編集:インテルコンパイラでMVS 2013を使用していることを忘れていました

4

1 に答える 1