使用YAML-CPP将YAML发射器内容保存到文件中,可以通过以下步骤实现:
git clone https://github.com/jbeder/yaml-cpp.git
cd yaml-cpp
mkdir build
cd build
cmake ..
make
sudo make install
#include<iostream>
#include <fstream>
#include <yaml-cpp/yaml.h>
int main() {
// 创建YAML发射器
YAML::Emitter out;
out << YAML::BeginMap;
out << YAML::Key << "name" << YAML::Value << "John Smith";
out << YAML::Key << "age" << YAML::Value << 30;
out << YAML::EndMap;
// 将YAML发射器内容保存到文件中
std::ofstream fout("output.yaml");
fout<< out.c_str();
fout.close();
return 0;
}
g++ main.cpp -o main -lyaml-cpp
./main
cat output.yaml
输出结果如下:
name: John Smith
age: 30
总结:使用YAML-CPP库可以方便地处理YAML数据,将YAML发射器内容保存到文件中只需要简单的几个步骤。
没有搜到相关的文章