利用boost/locale和ofstream写utf8格式文件

来源:互联网 发布:国密算法介绍 编辑:程序博客网 时间:2024/06/17 23:41

每次都在以前代码中找这段代码,现在放上来,方便查找。


#include <boost/program_options/detail/convert.hpp>  

#include <boost/program_options/detail/utf8_codecvt_facet.hpp> 
#include <boost/locale.hpp>

#include <fstream>



std::ofstream txtFile;        
        std::locale oldLocale; 
        std::locale utf8Locale(oldLocale,  new boost::program_options::detail::utf8_codecvt_facet());  
        txtFile.imbue(utf8Locale);
        txtFile.open(txtPath, std::ios::app);
        if (!txtFile)
        {
            throw -1;
        }
        txtFile<< boost::locale::conv::to_utf<char>(text,"gb2312");
        txtFile.close();

原创粉丝点击