-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.cpp
More file actions
executable file
·33 lines (26 loc) · 1.07 KB
/
encode.cpp
File metadata and controls
executable file
·33 lines (26 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// https://www.cnblogs.com/bigben0123/p/13728595.html
// https://sf-zhou.github.io/programming/chinese_encoding.html
// https://blog.csdn.net/wanggao_1990/article/details/113973730
#include <codecvt>
#include <fstream>
#include <iostream>
#define print(x) std::cout << x << std::endl
int main() {
const std::locale utf8( std::locale(), new std::codecvt_utf8<wchar_t> );
const std::wstring love_cpp = L"\x6211\x7231""C++"; // 我爱C++
std::wcout.imbue(utf8);
std::wcout << love_cpp << std::endl; // 输出 "我爱C++"
std::wcout << love_cpp.length() << std::endl; // 输出 5
std::wcout << love_cpp.substr(0, 2) << std::endl; // 输出 "我爱"
const auto k_output_filename = "test_02.txt";
std::wofstream f_out(k_output_filename, std::ios::out);
f_out.imbue(utf8);
f_out << love_cpp << std::endl;
f_out.close();
std::wifstream f_in(k_output_filename, std::ios::in);
f_in.imbue(utf8);
std::wstring value;
f_in >> value;
std::wcout << (value == love_cpp ? "Yes": "Opps") << std::endl; // 输出 "Yes"
f_in.close();
}