我正在制作一个程序,比如抽认卡,但基于控制台。在程序开始时,我从一个包含 UTF-8 编码的日文字符(例如"ひらがな, カタカナ, 患者"
)的文件中读取。但是,当我调用 时std::getline()
,输入输出为""
. 我怎样才能做到这一点?也许STD_INPUT_HANDLE
作为文件打开?我使用SetConsoleOutputCP()
and SetConsoleCP()
withCP_UTF8
作为参数来启用 UTF-8 打印。
@πάντα ῥεῖ要求的最小可重现示例
#include <iostream>
#include <Windows.h>
#include <fstream>
#include <vector>
#include <string>
void populate(std::vector<std::string>& in) {
std::ifstream file("words.txt"); // fill this with some UTF-8 characters, then check the contents of [in]
std::string line;
while (std::getline(file, line)) {
in.emplace_back(line);
}
}
int main() {
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
SetConsoleTitleA("Example");
std::vector<std::string> arr;
populate(arr);
std::string input_utf8; // type some UTF-8 characters when asked for input
std::cin >> input_utf8;
for (std::string s : arr)
if (input_utf8 == s)
std::cout << "It works! The input wasn't null!";
}