Reputation: 545
I'm trying to parse a YAML configuration file using yaml-cpp (https://github.com/jbeder/yaml-cpp), Visual Studio 2019 Community.
#include <iostream>
#include "yaml-cpp/yaml.h"
int main(int argc, char* argv[])
{
YAML::Node config;
try
{
YAML::Node config = YAML::LoadFile("conf.yml");
}
catch (YAML::BadFile e)
{
std::cerr << e.msg << std::endl;
return (1);
}
catch (YAML::ParserException e)
{
std::cerr << e.msg << std::endl;
return (1);
}
std::cout << config["window"] ? "Window found" : "Window not found" << std::endl;
return (0);
}
Here's my YAML file :
---
window:
width: 1280
height: 720
...
But the result is always :
Window not found
The loading is successful, but the content of the "config" node object seems empty. What am I doing wrong ?
Upvotes: 0
Views: 992
Reputation: 117318
You have variable shadowing:
YAML::Node config; // This is the config you print out at the end
try
{
// The below config is local to the narrow try-scope, shadowing the
// config you declared above.
YAML::Node config = YAML::LoadFile("conf.yml");
}
Correction:
YAML::Node config;
try
{
config = YAML::LoadFile("conf.yml");
}
Also put parentheses around your ternary operator:
std::cout << (config["window"] ? "Window found" : "Window not found") << '\n';
Upvotes: 2