I want to support UTF-8 string in my program, but the default active code page is 936.
Is there any methods to support UTF-8 without using the chcp 65001
command?
And the std::locale
doesn't seem to work, it always throws an error when I'm using std::locale::global(std::locale("zh_CN.UTF-8"));
:
terminate called after throwing an instance of 'std::runtime_error'
what(): locale::facet::_S_create_c_locale name not valid
Example:
#include <iostream>
#include <codecvt>
#include <locale>
int main() {
std::wstring a = L"中文字符串"; // a chinese string.
using cvt = std::codecvt_utf8<wchar_t>;
std::wstring_convert<cvt, wchar_t> converter;
std::string x = converter.to_bytes(a);
std::cout << x << std::endl;
return 0;
}
Output without "chcp 65001"
:
涓枃瀛楃涓
Output with "chcp 65001"
:
Active code page: 65001
中文字符串
I don't want to use the "chcp 65001" command and don't want to see the prompt.
How can I solve the problem?