C++ Boost在INI文件中找不到嵌套节点
我有一个像这样的ini文件C++ Boost在INI文件中找不到嵌套节点,c++,boost,ini,C++,Boost,Ini,我有一个像这样的ini文件 [Sensor] address=69 mode=1 [Sensor.Offsets] x=65.0 y=-66.3 我正在尝试在结构中加载值: #include <iostream> #include <string> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ini_parser.hpp> #include <
[Sensor]
address=69
mode=1
[Sensor.Offsets]
x=65.0
y=-66.3
我正在尝试在结构中加载值:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <spdlog/spdlog.h>
void myFunc() {
using namespace boost::property_tree;
ptree pt;
double x{};
// Read config file
boost::property_tree::ini_parser::read_ini("config.ini", &pt);
try {
x = pt.get<double>("Sensor.Offsets.x");
} catch (std::exception& ex) {
spdlog::error("{}", ex.what());
exit(-2);
}
}
#包括
#包括
#包括
#包括
#包括
void myFunc(){
使用名称空间boost::property_树;
ptree-pt;
双x{};
//读取配置文件
boost::property_tree::ini_parser::read_ini(“config.ini”,&pt);
试一试{
x=pt.get(“Sensor.Offsets.x”);
}捕获(标准::异常和例外){
spdlog::error(“{}”,例如what());
出口(-2);
}
}
我得到错误:没有这样的节点(Sensor.offset.x)
知道怎么回事吗?你需要“避开”这个点。点是特殊的,因此您的键被解释为[Sensor][offset][x],而不是[Sensor.offset][x]
你可以强制它:
auto& so = pt.get_child({"Sensor.Offsets", '#'});
x = so.get<double>("x");
印刷品
[Sensor.Offsets].(x,y) = (65,-66.3)
添加了现场演示,非常感谢您的时间:)
#include <boost/property_tree/ini_parser.hpp>
#include <fmt/format.h>
int main()
{
boost::property_tree::ptree pt;
std::istringstream config_ini("[Sensor]\naddress=69\nmode=1\n[Sensor.Offsets]\nx=65.0\ny=-66.3");
read_ini(config_ini, pt);
try {
auto& so = pt.get_child({"Sensor.Offsets", '#'});
double x = so.get<double>("x");
double y = so.get<double>("y");
fmt::print("[Sensor.Offsets].(x,y) = ({},{})\n", x, y);
} catch (std::exception& ex) {
fmt::print("{}\n", ex.what());
exit(-2);
}
}
[Sensor.Offsets].(x,y) = (65,-66.3)