读取ymal 文件common: msg_source: 1 #@type: int @detail: 0--not use Camera, 1--message come from sensor driver, 2--message come from ROS,3--message come from protobuf(UDP) send_msg_ros: true #@type: bool @detail: if true, the original Gnss message will be sent out through ROS send_msg_proto: false #@type: bool @detail: if true, the original Gnss message will be sent out through Proto camera_number: 4 camera_paths: camera0: include: ../sensor_config/other_sensors/camera/camera_C920e1.yaml camera1: include: ../sensor_config/other_sensors/camera/camera_C920e2.yaml camera2: include: ../sensor_config/other_sensors/camera/camera_C920e3.yaml camera3: include: ../sensor_config/other_sensors/camera/camera_C920e4.yaml
时间: 2024-04-15 17:27:26 浏览: 127
要读取上述YAML文件,您可以使用适合的YAML解析库,例如YAML-CPP或YAMLCPP-Parser。下面是使用YAML-CPP的示例代码:
```cpp
#include <iostream>
#include <yaml-cpp/yaml.h>
int main() {
// 加载YAML文件
YAML::Node config = YAML::LoadFile("common.yaml");
// 读取msg_source字段的值
int msgSource = config["msg_source"].as<int>();
std::cout << "msg_source: " << msgSource << std::endl;
// 读取send_msg_ros字段的值
bool sendMsgRos = config["send_msg_ros"].as<bool>();
std::cout << "send_msg_ros: " << std::boolalpha << sendMsgRos << std::endl;
// 读取send_msg_proto字段的值
bool sendMsgProto = config["send_msg_proto"].as<bool>();
std::cout << "send_msg_proto: " << std::boolalpha << sendMsgProto << std::endl;
// 读取camera_number字段的值
int cameraNumber = config["camera_number"].as<int>();
std::cout << "camera_number: " << cameraNumber << std::endl;
// 读取camera_paths字段的值
YAML::Node cameraPaths = config["camera_paths"];
for (const auto& camera : cameraPaths) {
std::string cameraName = camera.first.as<std::string>();
std::string includePath = camera.second["include"].as<std::string>();
std::cout << "camera name: " << cameraName << ", include path: " << includePath << std::endl;
}
return 0;
}
```
请确保将上述示例代码与YAML-CPP库一起编译,链接并运行。从YAML文件中读取的值将打印到控制台上。
阅读全文