我是C ++的新手。 我想在Visual Studio 2019中的C ++ 17中读取UTF-16文本文件。
我已经在Internet上尝试了几种方法(包括StackOverflow),但没有一种起作用,并且其中一些没有编译(我认为它们仅支持较旧的编译器)。
我试图在不使用任何第三方库的情况下实现这一目标。
这会读取一个文本文件,但是每个字母之间都有一些奇怪的字符和空格。
// open file for reading
std::wifstream istrm(filename, std::ios::binary);
if (!istrm.is_open()) {
std::cout << "failed to open " << filename << '\n';
}
else {
std::wstring s;
std::getline(istrm, s);
std::wcout << s << std::endl;
}
然后我使用以下库找到了一些解决方案
#include
#include
// open file for reading
std::wifstream istrm(filename, std::ios::binary);
istrm.imbue(std::locale(istrm.getloc(), new std::codecvt_utf16));
if (!istrm.is_open()) {
std::cout << "failed to open "