Parse YAML Files in C++(在 C++ 中解析 YAML 文件)
问题描述
我想要一个简单的教程来告诉我如何加载一个 yaml 文件并解析数据.Expat 风格会很棒,但任何能够以某种形式实际向我展示数据的解决方案都会很有用.
I want a simple tutorial to show me to load a yaml file and parse the data. Expat style would be great but any solution that actually shows me the data in some form would be useful.
到目前为止,我在 yaml-0.1.1 源代码中为 C 运行了多个测试,但我得到一个错误,没有任何输出,或者在 run-emitter.c 的情况下.它读入 yaml 文件并将其打印到 STDOUT,它不会通过 libyaml 函数/结构生成文本.在出现错误的情况下,我不知道是文件坏了还是我的构建不正确(我没有修改任何东西......)文件是从 yaml.org 复制的
So far I ran multiple test in yaml-0.1.1 source for C and I either get an error, no output what so ever or in run-emitter.c case. It reads in the yaml file and prints it to STDOUT, it does not produce the text via libyaml functions/structs. In the cases with an error I don't know if it was bc the file was bad or my build is incorrect (I didn't modify anything...) The file was copied from yaml.org
谁能指点我的教程?(我用谷歌搜索了至少 30 分钟,阅读了任何看起来相关的内容)或具有良好教程或示例的库的名称.也许您可以告诉我文件中加载了哪些 libyaml 测试并对其进行了处理,或者我为什么会出错.本文档不解释如何使用该文件,只解释如何加载它.
Can anyone point me to a tutorial? (I googled for at least 30mins reading anything that looked relevant) or a name of a lib that has a good tutorial or example. Maybe you can tell me which libyaml test loads in files and does something with it or why I gotten errors. This document does not explain how to use the file, only how to load it.
http://pyyaml.org/wiki/LibYAML#Documentation
推荐答案
试试yaml-cpp(如 this question 所建议)用于 C++ 解析器.
Try yaml-cpp (as suggested by this question) for a C++ parser.
披露:我是作者.
示例语法(来自 教程):
YAML::Node config = YAML::LoadFile("config.yaml");
if (config["lastLogin"]) {
std::cout << "Last logged in: " << config["lastLogin"].as<DateTime>() << "
";
}
const std::string username = config["username"].as<std::string>();
const std::string password = config["password"].as<std::string>();
login(username, password);
config["lastLogin"] = getCurrentDateTime();
std::ofstream fout("config.yaml");
fout << config;
这篇关于在 C++ 中解析 YAML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中解析 YAML 文件
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01