使用 C 脚本读取 YAML 文件
我想使用简单的 C 程序读取一个薄的 YAML 文件。我相信最好的方法是使用以下库:Pyyaml.org。
阅读维基页面后,我尝试在这里使用一些示例: http://pyyaml.org/browser/libyaml/branches/stable/tests/< /a> 但对于我这样的小白来说,却不是很容易理解。
例如,如果我得到这个更简单的 YAML 文件(名为 test.yml):
test:
- {conf: hi}
- {conf: test}
- {conf: abc}
我该如何做这样的事情:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/* I do not really see how to load the file here... */
char *conf[3] = {"hi", "test", "abc"}; /* configuration of the YAML file */
int i;
for (i = 0; i < 3; i++)
{
printf("content: %s\n", conf[i]);
}
return 0;
}
...但是从给定的 YAML 文件?
非常感谢您的任何建议!
I would like to read a thin YAML file using a simple C program. I beleve the best way for this is to use the follow library: Pyyaml.org.
After reading the wiki page, I have try to use some examples here:
http://pyyaml.org/browser/libyaml/branches/stable/tests/
But for a noob like me, it is not very simple to understand.
If for example I get this more simple YAML file (named test.yml):
test:
- {conf: hi}
- {conf: test}
- {conf: abc}
How can I do to do somethig like this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/* I do not really see how to load the file here... */
char *conf[3] = {"hi", "test", "abc"}; /* configuration of the YAML file */
int i;
for (i = 0; i < 3; i++)
{
printf("content: %s\n", conf[i]);
}
return 0;
}
...but from the given YAML file?
Many thanks for any suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
libyaml 似乎是基于事件的,这通常意味着它针对速度进行了调整,而不是为了易于使用而进行了调整。而且周围的例子并不多(至少对于原始的 C 代码来说,脚本语言绑定更流行)。源代码发行版的测试目录中有一些示例代码。最全面的似乎是解构器示例。它很长,但评论很好,所以很容易理解。
一般来说,我不确定 YAML 对于 C 代码有多大用处。 YAML 的最大优势在于它可以自然地映射到 Python 和 Perl 等脚本语言使用的数据类型 - 列表和字典,嵌套到任意级别。将此类嵌套结构简单地序列化到 YAML 中的能力使其很有吸引力。我很好奇为什么 C 代码需要 YAML。
libyaml seems to be event based, which usually means it's tuned for speed and less for easy usability. And there aren't many examples around (at least for the original C code, the scripting language bindings are more popular). The source distribution has some sample code in the tests directory. The most comprehensive seems to be the deconstructor example. It's long but very well commented, so it's easy to follow.
Generally, I'm not sure how useful YAML is for C code. YAML's greatest strength is that it maps naturally to the data types used by scripting languages like Python and Perl - lists and dictionaries, nested to arbitrary levels. The ability to trivially serialize such nested structures into YAML makes it attractive. I'm curious why you need YAML for C code.
这个问题的第二个答案有一个更好的使用示例将 yaml 文件解析为更有用的内容的库。
The second answer to this question has a little bit better example of using the library to parse yaml file into something more useful.