#include
#include
#include <yaml-cpp/yaml.h>
int main() {
try {
// Load the YAML file
std::ifstream file(“config.yaml”);
if (!file.is_open()) {
throw std::runtime_error(“Unable to open the YAML file”);
}
// Parse the YAML
YAML::Node config = YAML::Load(file);
if (!config.IsMap()) {
throw std::runtime_error("The root node of the YAML must be a map");
}
// Access and process the data
// Here, we assume the config file contains a key called "example_key"
if (config["example_key"]) {
std::string value = config["example_key"].as<std::string>();
std::cout << "Value of example_key: " << value << std::endl;
} else {
throw std::runtime_error("Missing required key: example_key");
}
} catch (const YAML::ParserException& ex) {
std::cerr << "YAML parser error: " << ex.what() << std::endl;
} catch (const YAML::BadConversion& ex) {
std::cerr << "YAML bad conversion error: " << ex.what() << std::endl;
} catch (const std::runtime_error& ex) {
std::cerr << "Runtime error: " << ex.what() << std::endl;
} catch (const std::exception& ex) {
std::cerr << "General error: " << ex.what() << std::endl;
}
return 0;
}