c++ - Boost::ini_parser:读取特定部分值的方法是什么?

标签 c++ windows boost ini

我正在使用 boost::property_tree 来读取 .ini 文件。

我知道我可以阅读特定的 key(inside section) -> iniTree.get<std::string>("section.key") .

我知道我可以读取 ini 文件中的所有值。

我只想读取特定部分的键。

类似的东西:iniTree.get<std::vector<std::string> >("section") .

这可能吗?

最佳答案

是的。您使用“get_child”获取子树。

如果您事先不知道该部分是否存在,您可以使用 get_child_optional

这是一个显示两种变体的演示:

Live On Coliru

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <fstream>
#include <iostream>

using boost::property_tree::ptree;

int main() {

    std::fstream ifs("input.txt");
    ptree pt;

    read_ini(ifs, pt);

    // let's get just the section2

    if (boost::optional<ptree&> oops = pt.get_child_optional("oops")) {
        std::cout << "There is a section `oops`\n";
    } else {
        std::cout << "There is NO section `oops`\n";
    }

    ptree& sub1 = pt.get_child("section2"); // we want the CCs!
    write_ini(std::cout, sub1);
}

给定一个 input.txt 为:

[section1]

huh=value1
slam=value2
cram=value3

[section2]

rabbits=creditcard1
die=creditcard2
eagerly=creditcard3

它将打印输出:

There is NO section `oops`
rabbits=creditcard1
die=creditcard2
eagerly=creditcard3

关于c++ - Boost::ini_parser:读取特定部分值的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31165532/

相关文章:

c++ - cpp异常获取抛出调用者的详细信息

通过构造函数为 vector 预分配 C++ 内存失败

.net - 如果某些进程需要分配超过 2 GB 的内存,会发生什么情况?

Windows批处理脚本来查看一系列服务器/端口是否打开?

c++ - scoped_ptr和shared_ptr可以混合使用吗?

python - 从哪里获得 boost/python.hpp?

c++ - 如何从 type_info 获取模板参数的类型

c++ - First Boost 计划

java - Maven Exec 插件不在 Windows 上使用系统路径?

c++ - 想要有效地克服 Boost.Interprocess 共享内存中映射中关键类型之间的不匹配