boost::property_tree解析,中文乱码如何处理呀
如题,当时没有用中文,所以今天才发现这个问题。
我的源码(自带例子)如下,请高手帮改下,
#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 version="1.0" encoding="utf-8"?> <debug> <filename> 大家好 </filename> <level> 4 </level> </debug> 望帮助~~~