编程工具学习篇--yaml-cpp

编程工具学习篇--yaml-cpp

前言

在日常编程中,免不了需要从外部读取/存储参数,这样操作也极大方便了参数的调整过程,有效提高了编程者的测试效率。

一、下载与安装

yaml-cpp下载链接:Download
在win下用Cmake编译code,不需要依赖别的库,只需要Configure然后Generate就可以,然后选择自己喜欢的IDE搞定就行。

二、使用步骤

主要目标:

  1. 从文件中读取参数
  2. 网文件中写入参数
#include <iostream>
#include <string>
#include <fstream>
#include <YAML_CPP/include/yaml.h>
using namespace std;

int main(int argc, char** argv)
{
	YAML::Node config = YAML::LoadFile("./config.yaml");

	cout << "Node type " << config.Type() << endl;
	cout << "skills type " << config["skills"].Type() << endl;

	cout << "name: " << config["name"].as<std::string>() << endl;
	cout << "age: " << config["age"].as<int>() << endl;
	cout << "name: " << config["skills"]["java"].as<int>() << endl;

	std::ofstream fout("./test.yaml");
	config["score"] = 100;
	fout << config;
	fout.close();

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值