5

I have a problem with multibyte char strings. I have simplified my problem as below:

std::wstring str = L"multıbyte test string";
std::wofstream f;
f.open("F:\\dump.txt");
f << str;
f.close();

and the dump file's content is : "mult"

Why does it cuts the remaining part of str altough i have used wstring and wofstream?

Thanks

ST3
  • 8,826
  • 3
  • 68
  • 92
xyzt
  • 1,201
  • 4
  • 18
  • 44

2 Answers2

1

wofstream writes out data using the current locale. The default locale probably does not support the multibyte characters.

See question: Unable to write a std::wstring into wofstream

You can get it to output the full string by:

std::locale::global(std::locale(""));

before writing, however you won't get the characters as unicode on windows, since it doesn't support UTF-8 locales natively.

To do that, you should convert it to a std::string using WideCharToMultiByte, and write it out using regular ofstream.

Community
  • 1
  • 1
yiding
  • 3,482
  • 18
  • 17
0

You will have to imbue the output stream with some locale to get some reasonable codecvt facet to do wchar_t to char conversion. If you have C++11 supporting compiler or Visual Studio 2010 and later, you can use UTF-8 facet (codecvt_utf8):

f.imbue(
    std::locale (         // using std::locale constructed from
        std::locale (),   // global locale
                          // and codecvt_utf8 facet
            new std::codecvt_utf8<char, 0x10FFFF,
                static_cast<std::codecvt_mode>(std::consume_header
                    | std::little_endian)>);

There is also codecvt_utf16.

wilx
  • 17,697
  • 6
  • 59
  • 114