Correctly reading a utf-16 text file into a string without external libraries?

后端 未结 3 1378
醉酒成梦
醉酒成梦 2020-11-29 05:24

I\'ve been using StackOverflow since the beginning, and have on occasion been tempted to post questions, but I\'ve always either figured them out myself or found answers pos

3条回答
  •  孤独总比滥情好
    2020-11-29 06:01

    The C++11 solution (supported, on your platform, by Visual Studio since 2010, as far as I know), would be:

    #include 
    #include 
    #include 
    #include 
    int main()
    {
        // open as a byte stream
        std::wifstream fin("text.txt", std::ios::binary);
        // apply BOM-sensitive UTF-16 facet
        fin.imbue(std::locale(fin.getloc(),
           new std::codecvt_utf16));
        // read     
        for(wchar_t c; fin.get(c); )
                std::cout << std::showbase << std::hex << c << '\n';
    }
    

提交回复
热议问题