0

May be the best way to describe, what I would like to achieve, is to combine solutions from following threads: Windows Unicode C++ Stream Output Failure (but with utf-16)

and

How to print a number with a space as thousand separator? (but with dot as separator)

The following solution attempts had no effect on output:

//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
}

also, since MVS/windows already uses UTF16 for wide strings, i wonder why a conversion to utf16 is necessary at all. As I see it, it is a waste of the CPU resources. Is it not possible to write file without the extra unnecessary conversion? writing in binary mode is possiblity, but kind of silly, because it would forfeit all conveniences output stream provide

EDIT: forgot to mention that I use MVS 2013 with Intel compiler

Community
  • 1
  • 1
Andrey Pro
  • 501
  • 1
  • 10
  • 22

1 Answers1

1

The second variant works, once you make do_grouping const, as in

string do_grouping() const { return "\3"; }

As written, yours overloads, but doesn't override, the base class method.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85