【发布时间】:2018-10-16 17:48:38
【问题描述】:
我正在尝试在控制台中显示一个中文文本,它已从维基百科粘贴到一个 .txt 文件中(我不知道编码,也许是 UTF-8?)
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <codecvt>
using namespace std;
int main () {
const locale utf8_locale
= locale(locale(), new std::codecvt_utf8<wchar_t>());
std::wifstream file("dao.txt");
file.imbue(utf8_locale);
wstring s;
if (file.is_open())
{
while (getline(file, s))
{
cout << s << '\n';
// Do something with the string
}
else cout << "Unable to open file";
myfile.close();
}
return 0;
}
我收到: 错误:'operator
为什么没有重载?
【问题讨论】:
-
用std::wcout怎么样?
-
您必须知道编码才能正确显示。
-
您的终端期望什么编码?
UTF-8?如果是这样,只需将其作为普通的std::string读入(不需要刻面)并再次打印出来。UTF-16?然后您可以尝试打印到(并从中读取)std::wcout?底线是您必须输出与显示终端所期望的相同的编码。