Save a YAML Emitter content to a file with YAML-CPP

試著忘記壹切 提交于 2019-12-23 10:59:41

问题


I just started playing around with yaml-cpp, I managed to build it properly and run some of the examples from the yaml-cpp wiki but I can't find a way to save my emitter to a file.

Is this not possible? I mean the PyYAML library has a 'dump' function for this. Is there no such functionality in yaml-cpp? Is there some workaround to converting a yaml emitter to a stl stream and then dumping this to a yaml file?

Please let me know

Thanks, Adam


回答1:


The function Emitter::c_str() returns a NULL-terminated C-style string (which you do not have to release), which you can then write to a file. For example:

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

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

There is also Emitter::size(), which returns the number of bytes in that string, in case you want to do something more advanced and don't want to walk the string to find its length.

If you want to just dump a Node to a stream, there's a shortcut:

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


来源:https://stackoverflow.com/questions/3342355/save-a-yaml-emitter-content-to-a-file-with-yaml-cpp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!