这其实非常简单,我们可以使用char32_t来匹配32位的Unicode的字符,代码例子如下:
#include <iostream>
#include <fstream>
#include<filesystem>
int main() {
std::ofstream of{ "test.txt" };
static constexpr auto& str = U"嘿嘿归故里";
of.write((const char*)str, sizeof(str));
of.close();
std::ifstream is{ "test.txt" };
char32_t buff[sizeof(str) / sizeof(char32_t)]{};
is.read((char*)buff, sizeof(str));
std::cout << std::filesystem::path(buff).string();
is.close();
}
我们使用的是c++17的filesystem的库进行编码转换,U是表示32位Unicode的字面量