STL and UTF-8 file input/output. How to do it?

前端 未结 5 445
醉酒成梦
醉酒成梦 2020-12-06 08:15

I use wchar_t for internal strings and UTF-8 for storage in files. I need to use STL to input/output text to screen and also do it by using full Lithua

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 08:49

    Use std::codecvt_facet template to perform the conversion.

    You may use standard std::codecvt_byname, or a non-standard codecvt_facet implementation.

    #include 
    using namespace std;
    typedef codecvt_facet Cvt;
    locale utf8locale(locale(), new codecvt_byname ("en_US.UTF-8"));
    wcout.pubimbue(utf8locale);
    wcout << L"Hello, wide to multybyte world!" << endl;
    

    Beware that on some platforms codecvt_byname can only emit conversion only for locales that are installed in the system.

提交回复
热议问题