C++使用yaml配置文件

使用配置文件有利于我们灵活配置工程,解决大量重复劳动,也方便调试

读取config.yaml文件中参数内容,并赋值给程序中相关参数。

 

如果不存在key,会报错:

terminate called after throwing an instance of 'YAML::TypedBadConversion'

  what():  yaml-cpp: error at line 0, column 0: bad conversion

已放弃 (核心已转储)

 

示例:

yaml_test.cpp

#include 
#include "yaml-cpp/yaml.h"
#include 
 
using namespace std;
 
typedef struct
{
  int cpp;
  int java;
  int android;
  int python;

}Skills;


typedef struct
{
    string name;
    string sex;
    int age;
    Skills skills;

} Config;



int test_base()
{
    YAML::Node config;
    try{
         config = YAML::LoadFile("../config.yaml");
    }catch(YAML::BadFile &e){
        std::cout<<"read error!"<() << endl;
 
    cout << "name:" << config["name"].as() << endl;
    cout << "sex:" << config["sex"].as() << endl;
    cout << "age:" << config["age"].as() << endl;
 
    //读取不存在的node值,报YAML::TypedBadConversion异常
    try{
        string label = config["label"].as();
    }catch(YAML::TypedBadConversion &e){
        std::cout<<"label node is NULL"<() << endl;
    cout << "skills java:" << config["skills"]["java"].as() << endl;
    cout << "skills android:" << config["skills"]["android"].as() << endl;
    cout << "skills python:" << config["skills"]["python"].as() << endl;
 
    for(YAML::const_iterator it= config["skills"].begin(); it != config["skills"].end();++it)
    {
        cout << it->first.as() << ":" << it->second.as() << endl;
    }
 
    YAML::Node test1 = YAML::Load("[1,2,3,4]");
    cout << " Type: " << test1.Type() << endl;
 
    YAML::Node test2 = YAML::Load("1");
    cout << " Type: " << test2.Type() << endl;
 
    YAML::Node test3 = YAML::Load("{'id':1,'degree':'senior'}");
    cout << " Type: " << test3.Type() << endl;
 
    ofstream fout("./testconfig.yaml"); //保存config为yaml文件
 
    config["score"] = 99;//添加新元素
 
    fout << config;
 
    fout.close();
 
    return 0;
}



int use_yaml_config()
{
    YAML::Node config;
    try{
         config = YAML::LoadFile("../config.yaml");
    }catch(YAML::BadFile &e){
        std::cout<<"read error!"<();
    param.name = config["name"].as();
    param.sex = config["sex"].as();

    param.skills.android = config["skills"]["android"].as();
    param.skills.java = config["skills"]["java"].as();
    param.skills.python = config["skills"]["python"].as();
    param.skills.cpp = config["skills"]["cpp"].as();


    //查看结果
    cout<< " param.age = "<:后面一定要加空格哦

CMakeLists.txt 配置


cmake_minimum_required(VERSION 3.2)
project(yaml_test)
add_definitions(-std=c++11)


find_package(yaml-cpp CONFIG REQUIRED)

set(SRCS ./src/yaml_test.cpp)

add_executable(yamltest ${SRCS})

# depend on catkin package

target_link_libraries(yamltest ${catkin_LIBRARIES} yaml-cpp) 


 

C++使用yaml配置文件_第1张图片

mkdir build
cd build
cmake ..
make
./yamltest

执行结果:

yuan@linx-c:~/VSCode/CPP_Learning/yaml/yaml_test/build$ ./yamltest 
 param.age = 18
 param.name = frank
 param.sex = male
param.skills.android = 3
 param.skills.java = 2
 param.skills.python = 4
 param.skills.cpp = 1

参考:

C++ 解析yaml文件的使用方法 https://blog.csdn.net/sunlin972913894/article/details/103038082

c++ 开发中利用yaml-cpp读写yaml配置文件 https://blog.csdn.net/briblue/article/details/89515470

 

Node 是 yaml-cpp 中的核心概念,它用于存储解析后的 yaml 信息。

yaml 支持 Scalar、List、Map 类型,yaml-cpp 通过 NodeType 定义了 Node 的可能类型。

namespace YAML {
struct NodeType {
  enum value { Undefined, Null, Scalar, Sequence, Map };
};
}

对应未定义、空、标量、序列、字典。

 

你可能感兴趣的:(C++,Ubuntu)