在C++管理一家中国.txt
Manage a Chinese .txt in C++
我正在尝试在控制台中显示一个 cinese 文本,它已从维基百科粘贴到.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;
}
我收到:错误:与"运算符<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream}"和"std::__cxx11::wstring {aka std::__cxx11:::basic_string}"(|
为什么<<</em>没有过载?
有几个错误:
否则不匹配,如果
if (file.is_open())
{
...
else cout << "Unable to open file";
...
}
使用 std::wcout。s
是一个宽字符串。使用宽输出。
std::wcout << s << 'n'; // not std::cout notice the w
没有变量叫myfile
我的意思可能是file
/*my*/file.close();
注意:
在打开溪流之前,您必须灌输它。
std::wifstream file;
file.imbue(utf8_locale);
file.open("dao.txt");
问题是,如果从文件中读取了任何字符,则灌输将失败。一些实现会检查是否有 BOM 标记(如果没有,则放回字符(。但是检查这些 BOM 字符意味着文件已被读取,因此会使输入失败。因此,在打开之前始终灌注文件。
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 一种在C++中读取TXT配置文件的简单方法
- 如何修复此iFile以将.txt输出到控制台
- 无法打开.txt文件
- CMakeLists.txt中的命名空间表示法
- Arduino从Txt读取整数
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 使用fstream对txt文件中的数字进行循环
- Visual Studio在尝试读取resource.txt文件时崩溃
- 更正 CMakeList.txt 用于谷歌测试的文件?
- 如何从txt文件中读取多个不同长度的数组?
- 在一次迭代中从 txt 文件中读取多行
- 将生成文件转换为 CMakeLists.txt等效项
- 使用循环将多行写入.txt
- CMake 错误"源似乎不包含 CMakeLists.txt",路径/库连接问题
- C++/autoconf 等效于文档库要求.txt的要求是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 在C++管理一家中国.txt