使用YAML-CPP将YAML发射器内容保存到文件中 [英] Save a YAML Emitter content to a file with YAML-CPP
问题描述
我刚刚开始使用yaml-cpp,我设法正确地构建了它,并运行了yaml-cpp Wiki中的一些示例,但是我找不到将发射器保存到文件中的方法.
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.
这不可能吗?我的意思是PyYAML库为此提供了一个转储"功能. yaml-cpp中没有这种功能吗? 是否有一些变通办法将yaml发射器转换为stl流,然后将其转储到yaml文件?
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?
请让我知道
谢谢, 亚当
推荐答案
函数Emitter::c_str()
返回以NULL
结尾的C样式字符串(您不必释放该字符串),然后可以将其写入到文件.例如:
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();
还有Emitter::size()
,它返回该字符串中的字节数,以防您想执行更高级的操作并且不想遍历字符串以查找其长度.
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.
如果您只想将Node
转储到流中,则有一个快捷方式:
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;
这篇关于使用YAML-CPP将YAML发射器内容保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!