如何在C ++中读取UTF-8文件数据? [英] How to read UTF-8 file data in C++?

查看:623
本文介绍了如何在C ++中读取UTF-8文件数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of IPA (UTF-8) symbols in a text file called ipa.txt with numbers assigned to them. How do I cross reference it with a source file which is also a text file that contains a bunch of words and their corresponding IPA, to return a text file for every names with their names as their filename and inside the text file should contain their corresponding numbers of IPA.

以下是我尝试过但没有奏效的内容,仅输出大部分为000000.

Below is what I've tried but didn't work, only outputs were mostly 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,则应使用:

I believe you are using the wrong type for c used in the iteration over name. As c is used as key for the map, and name is a wstring, you should use:

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

代替:

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

不是吗?

希望这会有所帮助,斯特凡诺

Hope this may help, Stefano

这篇关于如何在C ++中读取UTF-8文件数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆