我对多字节 char 字符串有疑问。我已将我的问题简化如下:
std::wstring str = L"multıbyte test string";
std::wofstream f;
f.open("F:\\dump.txt");
f << str;
f.close();
转储文件的内容是:“mult”
虽然我已经使用了 wstring 和 wofstream,但为什么它会削减 str 的剩余部分?
谢谢
最佳答案
wofstream
使用当前语言环境写出数据。默认语言环境可能不支持多字节字符。
参见问题:Unable to write a std::wstring into wofstream
您可以通过以下方式让它输出完整的字符串:
std::locale::global(std::locale(""));
在编写之前,但是你不会在 Windows 上将字符作为 unicode,因为它本身不支持 UTF-8 语言环境。
为此,您应该使用 WideCharToMultiByte
将其转换为 std::string,并使用常规 ofstream
将其写出。
关于c++ - 在 C++ 中输出多字节字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951188/