UTF-8 输入问题
Trouble with UTF-8 input
我的简单代码在输出日文字符时没有问题,但由于某些原因它无法正确输入,是不是缺少什么?
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_
标志没有太多解释...
#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
你好马克!
我的简单代码在输出日文字符时没有问题,但由于某些原因它无法正确输入,是不是缺少什么?
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_
标志没有太多解释...
#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
你好马克!