c++ - 将 std::u16string 写入文件?

标签 c++ string c++11 wstring

我正在尝试将 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/

相关文章:

c++将序列上限传递给可变函数模板

c++ - 带有对象的初始化列表是不可移植的?

c++ - 为什么编译器接受这个删除的 move 构造函数?

C++类型漏洞解释

c++ - c 和 c++ 与库的链接

c++ - 如何通过引用传递对象以供另一个文件使用

javascript - 在这种情况下,多维数组值分配如何工作?

javascript - 如何验证JavaScript中的某个字符是否在两个专用字符内?

c - 更快的strlen?

c++ - 如何在 C++ 中用另一个较小的 vector 填充 vector ?