I want to use yaml-cpp for a project of mine to generate a yaml-file however I have trouble figuring out how exactly I have to go about this. The yaml file I need to emit should look like this:
action_counts:
version: 0.3
subtree:
- name: system
local:
- name: adder
action_counts:
- name: add
counts: 1000
- name: idle
counts: 10000
So far I haven't been able to write the last four lines. I know that a '-' represents an array but I don't know how I can print the name and counts keys like that.
I have written some bits of code to experiment with yaml-cpp. The code looked like this:
YAML::Node node;
node["action_counts"] = YAML::Null;
node["action_counts"]["version"] = "0.3";
node["action_counts"]["subtree"].push_back("system");
std::ofstream fout("fileUpdate.yaml");
fout << node;
return 0;
And the output it produces is this:
action_counts:
version: 0.3
subtree:
- system
The last line here is wrong but I haven't managed to find out how to print - name: system
instead of this. How do I do this?
Once thats done how do I print the rest? Is local
a part of the - name
array? I think once I know how to format those to two lines I can figure out the rest by myself.
Lots of examples of creating messages can be found in the tests of yaml-cpp
library.
The desired structure can be formed as follows:
#include <fstream>
#include "yaml-cpp/yaml.h"
int main() {
YAML::Node action_1;
action_1["name"] = "add";
action_1["counts"] = 1000;
YAML::Node action_2;
action_2["name"] = "idle";
action_2["counts"] = 10000;
YAML::Node local_item;
local_item["name"] = "adder";
local_item["action_counts"].push_back(action_1);
local_item["action_counts"].push_back(action_2);
YAML::Node local;
local.push_back(local_item);
YAML::Node subtree_item;
subtree_item["name"] = "system";
subtree_item["local"] = local;
YAML::Node root;
root["action_counts"]["version"] = "0.3";
root["action_counts"]["subtree"].push_back(subtree_item);
std::ofstream fout("fileUpdate.yaml");
fout << root;
return 0;
}