Post

利用boost::property_tree读取INI文件

通过读取INI文件的实例学习boost::property_tree类的用法

利用boost::property_tree读取INI文件

Example

Test.ini

1
2
[SERVER]
IpAddr=127.0.0.1

Test.cpp

1
2
3
4
5
6
7
8
9
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
void func(){
    boost::property_tree::ptree pt;
    boost::property_tree::ini_parser::read_ini("./Test.ini", pt);
    //在windows上vs编译后出现ipStr为乱码,原因未知,改用string即可正常处理
    //char *ipStr = pt.get<std::string>("SERVER.IpAddr").c_str();
    std::string ip = pt.get<std::string>("SERVER.IpAddr");
}

参考

xml/json 等更多的使用方法请跳转:
boost::property_tree 的官方描述
boost::property_tree 的方法

This post is licensed under CC BY 4.0 by the author.