yaml-cpp 库 demo

关于eclipse工程配置,见下文链接。

https://blog.csdn.net/CAIYUNFREEDOM/article/details/89680681

代码托管

https://github.com/sofiathefirst/imagesCpp/blob/master/04yamlLearn/yamldemo.cpp

yaml文件内容

username: zym
password: 345zaA
age: 23
money: 4000.45

yaml-cpp 库 demo_第1张图片

#include "yaml-cpp/yaml.h"
#include 
#include
using namespace std;
int main() {
	YAML::Node config = YAML::LoadFile(
			"/home/q/eclipse-workspace/yamldemo/test.yaml");
	const std::string username = config["username"].as();
	const std::string password = config["password"].as();
	int age = config["age"].as();
	float money = config["money"].as();
	cout << username << endl << password << endl << age << endl << money
			<< endl;

	YAML::Emitter out;
	//out << YAML::BeginSeq;
	//out << YAML::Anchor("fred");
	out << YAML::BeginMap;
	out << YAML::Key << "name" << YAML::Value << "Fred";
	out << YAML::Key << "age" << YAML::Value << "42";
	out << YAML::EndMap;
	//out << YAML::Alias("fred");
	//out << YAML::EndSeq;

	std::ofstream fout("/home/q/eclipse-workspace/yamldemo/writer.yaml");
	fout << out.c_str();
	fout.close();

	return 0;
}

writer.yaml文件内容

name: Fred
age: 42

 

 

----------------------------

yaml-cpp 库 demo_第2张图片

 

-----------------------------------

yaml-cpp 库 demo_第3张图片

 

你可能感兴趣的:(linux,eclipse,cpp)