我正在尝试将 std::u16string 写入文件
它可以很容易地用 std::string 完成
// ... pseudocode
std::wofstream outfile;
outfile.open("words.txt", std::ios_base::app);
outfile << "something";
outfile.close();
// ...
但是我怎样才能将 std::u16string 写入文件呢?
谢谢
最佳答案
需要创建一个basic_ofstream
,其char类型等同于u16string
值类型,应该是char16_t
:
typedef std::basic_ofstream<char16_t> u16ofstream;
u16ofstream outfile("words.txt", std::ios_base::app);
outfile << someu16string;
outfile.close();
或者,您可以将 u16string
转换为常规 string
并将其写入普通 ofstream
,但您必须处理转换并自行处理字符串的编码。
wofstream
也可能与某些平台(特别是 Windows)上的 u16string
兼容,但它不可移植。
关于c++ - 将 std::u16string 写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35324237/