Save the contents of the YAML emitter to a file using YAML-CPP

I just started playing with yaml-cpp, I managed to create it correctly and run some of the yaml-cpp wiki examples, but I can’t find a way to save my emitter to a file.

Is it impossible? I mean, the PyYAML library has a dump function for this. Is there such a function in yaml-cpp? Is there a way around the conversion of a yaml emitter to a stl stream and then dumping it into a yaml file?

Please let me know

Thanks Adam

+3
source share
1 answer

The function Emitter::c_str()returns the string NULL-terminated C-style (which you should not let go), which you can then write to the file. For instance:

YAML::Emitter emitter;
emitter << "Hello world!";

std::ofstream fout("file.yaml");
fout << emitter.c_str();

Emitter::size(), , - , , .

Node , :

YAML::Node node = ...;
std::ofstream fout("file.yaml");
fout << node;
+4

Source: https://habr.com/ru/post/1756720/


All Articles