核心代码如下,实现了读取某个yaml文件,增加某项,在输出到文件中的作用。
#include "yaml-cpp/yaml.h"
double res;
std::string str;std::ifstream fin("/home/robot/test.yaml");
YAML::Node doc = YAML::Load(fin);
doc["resolution"] >> res;
doc["mytest"] = "g";
doc["resolutiond"] >> str;
ROS_INFO("resolution:%f,%d",res,doc.size());
ROS_INFO("mytest:%s",str.c_str());
std::ofstream fout("/home/robot/test1.yaml");
fout << doc;
需要注意在CMakefile.txt中target_link_libraries中增加yaml-cpp库。
https://github.com/jbeder/yaml-cpp/wiki/Tutorial
未完待续...