I am making a program, like flashcards but console based. At the start of the program, I read from a file containing UTF-8 encoded Japanese characters (such as "ひらがな, カタカナ, 患者"
). However, when I call std::getline()
, the input comes out as ""
. How can I achieve this? Maybe opening STD_INPUT_HANDLE
as a file? I use SetConsoleOutputCP()
and SetConsoleCP()
with CP_UTF8
as an argument to enable UTF-8 printing.
Minimal Reproducible Example, as requested by @πάντα ῥεῖ
#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!";
}