C++ wifstream读取日文方法(中文适用)

#include <iostream>
#include <fstream>
#include <locale>
#include <codecvt>

int main()
{
    
    const std::locale empty_locale = std::locale::empty();
    typedef std::codecvt_utf8<wchar_t> converter_type;  //std::codecvt_utf16
    const converter_type* converter = new converter_type;
    const std::locale utf8_locale = std::locale(empty_locale, converter);

    std::wifstream fin("Test_GetNextChar.txt");  //input
    fin.imbue(utf8_locale);

    wchar_t ch;
    while (!fin.eof())
    {
        fin >> ch;
        std::cout << ch << std::endl;
    }

    system("pause");
    return 0;
}

方法来自:https://www.zhihu.com/question/29043383

posted @ 2016-05-19 13:33  elninovt9  阅读(1602)  评论(0)    收藏  举报