假设有如下配置文件 Net.ini
[netinfo]
ip=192.168.1.2
port=1234
boost库提供了读写ini的方法,
namespace ini_parser
{
template<class Ptree>
void read_ini(std::basic_istream<typename Ptree::key_type::value_type> &stream,
Ptre &pt);
template<class Ptree>
void write_ini(std::basic_ostream<typename Ptree::key_type::value_type> &stream,
const Ptree &pt,
int flags = 0);
}
在使用时需要包含相应的头文件以及命名空间,相比windows的读写ini方法,boost的操作更简单。下面提供boost库读写ini文件的代码。
用VS2017建立windows控制台程序,包含boost库目录,以及lib库目录,不清楚的可以看我之前的boost库博客,这里不再累述。
代码如下:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <string>
#include <iostream>
using namespace std;
using nam