Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

outputing multibyte string in C++

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

like image 450
xyzt Avatar asked Nov 12 '22 14:11

xyzt


1 Answers

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.

like image 181
yiding Avatar answered Nov 16 '22 23:11

yiding