读取配置文件中的var

时间:2017-07-22 11:12:54

标签: c++

这是我需要阅读的文件。

id=1    
Name=test
Path=/var/www

我可以这样读这个文件:

std::ifstream f("/home/atmoner/conf.ini");
std::string s;

但是如何用c ++读取var Path

1 个答案:

答案 0 :(得分:0)

Boost option

std::string path;
boost::program_options::variables_map vm;
boost::program_options::options_description desc(
    "Allowed options");

desc.add_options()
    ("Path", boost::program_options::value<std::string>(&path), "document here...");

// boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
boost::program_options::store(boost::program_options::parse_config_file<char>("a.ini", desc), vm);
notify(vm);

//now variable path will contain the value.

Fplus option

string line = "Path=/var/www";
std::vector<std::string> vs = fplus::split('=', false, line);
string key = vs[0];
string value = vs[1];

手写:

std::string s="Path=/var/www";
string::iterator it = std::find(s.begin(), s.end(), '=');
if(it!=s.end())
{
    std::string key(s.begin(), it);
    std::string value(it+1, s.end());
}