UTF-8输入有问题 [英] Trouble with UTF-8 input
本文介绍了UTF-8输入有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的简单代码输出时日语字符没有问题,但是由于某种原因,它不能正确输入,是否缺少某些东西?
My simple code has no trouble with Japanese characters when outputting, but for some reason it doesn't take input properly, is it lacking something?
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT);
SetConsoleCP(CP_UTF8);
std::wstring s = L"こんにちは, 世界!\nHello, World!";
std::wcout << s << endl;
std::wstring test;
getline(wcin, test);
std::wstring test2 = test;
std::wcout << test2 << endl;
std::wstring test3 = test2;
std::wcout << test3 << endl;
std::wcout << "Press ENTER to exit.";
std::wcout << "\n";
cin.get();
return 0;
}
推荐答案
此代码在Windows 10命令提示符下对我有用.关于 _O _
标志的解释不多...
This code worked for me in the Windows 10 command prompt. There isn't much explanation about the _O_
flags...
#include <fcntl.h>
#include <io.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
_setmode(_fileno(stdout), _O_U16TEXT); // _O_WTEXT also worked
_setmode(_fileno(stdin), _O_WTEXT); // only _O_WTEXT worked
wstring s = L"こんにちは, 世界!\nHello, World!";
wcout << s << endl;
wstring test;
getline(wcin, test);
wcout << test << endl;
return 0;
}
输出:
C:\>test
こんにちは, 世界!
Hello, World!
你好马克! << input line
你好马克!
这篇关于UTF-8输入有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文