可能是最好的描述方式,我想实现的是结合以下线程的解决方案: 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 和英特尔编译器