在 C++ 中解析 YAML 文件

Parse YAML Files in C++(在 C++ 中解析 YAML 文件)

本文介绍了在 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 文件

基础教程推荐