c++ - 如何在 C++ 中读取 UTF-8 文件数据?

标签 c++ utf-8 io

我有一个 IPA 的列表(UTF-8) 符号在名为 ipa.txt 的文本文件中,并为其分配了数字。我如何将它与源文件交叉引用,该源文件也是一个包含一堆单词及其相应 IPA 的文本文件,以返回每个名称的文本文件,其名称作为文件名,文本文件中应包含相应的文本文件IPA 的数量。

下面是我试过但没有用的,只有输出大部分是 000000。

int main()
{
    std::unordered_map <wchar_t, int> map;
    std::wifstream file;
    file.open("ipa.txt");
    if (file.is_open()) {
        std::cout << "opened ipa file";
    }

    wchar_t from;
    int to;
    while (file >> from >> to) {
        map.insert(std::make_pair(from, to));
    }

    std::wifstream outfile;
    outfile.open("source.txt");
    if (outfile.is_open()) {
        std::cout << "opened source file";
    }

    std::wstring id;
    std::wstring name;
    while (outfile >> id >> name) {
        std::ofstream outputfile;
        outputfile.open(id + L".txt");
        for (wchar_t c : name)  outputfile << map[c]; 
    }

    system("pause");

    return 0;
}

最佳答案

我相信您在 name 的迭代中使用了错误的 c 类型。由于 c 用作映射的键​​,而 namewstring,您应该使用:

for (wchar_t c : name)  outputfile << map[c]; 

代替:

for (char c : name)  outputfile << map[c]; 

不是吗?

希望这会有所帮助,Stefano

关于c++ - 如何在 C++ 中读取 UTF-8 文件数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44549101/

相关文章:

c++ - 未显示 Char 变量值

c++ - 检查socket是否连接

C++:这个磁盘寻道会对性能造成很大影响吗?

c# - 快速轻松地从一个文件加载所需内容?

c++ - 有符号/无符号不匹配和函数在转换为函数时不带 2 个参数

mysql - Navicat 查询 - 从 utf8 到 varbinary

html - HTML 页面标题中的西类牙字符

c++ - UTF8 到/从 STL 中的宽字符转换

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

C++14 shared_timed_mutex VS C++11 互斥量