首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

boost:property_tree解析,中文乱码如何避免呀

2012-05-27 
boost::property_tree解析,中文乱码如何处理呀如题,当时没有用中文,所以今天才发现这个问题。我的源码(自带

boost::property_tree解析,中文乱码如何处理呀
如题,当时没有用中文,所以今天才发现这个问题。
我的源码(自带例子)如下,请高手帮改下,

C/C++ code
#include <boost/property_tree/ptree.hpp>  #include <boost/property_tree/xml_parser.hpp>  #include <boost/foreach.hpp>  #include <string>  #include <set>  #include <exception>  #include <iostream>  struct debug_settings  {      std::string m_file;               // log filename      int m_level;                      // debug level      std::set<std::string> m_modules;  // modules where logging is enabled      void load(const std::string &filename);      void save(const std::string &filename);  };  void debug_settings::load(const std::string &filename)//注意这是pass by reference方式  {      // Create empty property tree object      using boost::property_tree::ptree;      ptree pt;      read_xml(filename, pt);      m_file = pt.get<std::string>("debug.filename");      m_level = pt.get("debug.level", 0);  }  void debug_settings::save(const std::string &filename)  {      // Create empty property tree object      using boost::property_tree::ptree;      ptree pt;      // Put log filename in property tree      pt.put("debug.filename", m_file);      // Put debug level in property tree      pt.put("debug.level", m_level);      BOOST_FOREACH(const std::string &name, m_modules)          pt.add("debug.modules.module", name);      // Write property tree to XML file      write_xml(filename, pt);  }  int main()  {      try      {          debug_settings ds;          ds.load("debug_settings.xml");          ds.save("debug_settings_out.xml");          std::cout << "Success\n";      }      catch (std::exception &e)      {          std::cout << "Error: " << e.what() << "\n";      }      return 0;  }  


xml文件有中文情况,
XML code
<?xml version="1.0" encoding="utf-8"?>  <debug>      <filename>          大家好      </filename>         <level>          4      </level>    </debug>    望帮助~~~


[解决办法]
因为xml是UTF-8的,所以你要转为UINICODE,或者ACP的ansi。
[解决办法]
探讨
因为xml是UTF-8的,所以你要转为UINICODE,或者ACP的ansi。

[解决办法]
如果是在windows下,UTF8的中文很成问题

热点排行