C++:
Working with YAML

How to:

To work with YAML in C++, a popular choice is the yaml-cpp library. First, ensure you have yaml-cpp installed and properly linked to your C++ project.

Reading a YAML file:

#include <iostream>
#include <fstream>
#include <yaml-cpp/yaml.h>

int main() {
    YAML::Node config = YAML::LoadFile("config.yaml");
    
    if(config["title"]) {
        std::cout << "Title: " << config["title"].as<std::string>() << std::endl;
    }
    
    return 0;
}

Given a config.yaml that looks like this:

title: "Example YAML"

Running the above C++ code would produce:

Title: Example YAML

Writing to a YAML file:

#include <fstream>
#include <yaml-cpp/yaml.h>

int main() {
    YAML::Emitter out;
    out << YAML::BeginMap;
    out << YAML::Key << "title" << YAML::Value << "Example YAML";
    out << YAML::EndMap;
    
    std::ofstream fout("output.yaml");
    fout << out.c_str();
    
    return 0;
}

This code will create an output.yaml with the content:

title: Example YAML

These examples serve as a basic introduction to reading from and writing to YAML files in C++ using the yaml-cpp library. For more complex structures and use cases, explore the yaml-cpp documentation for features like sequences, tags, and more advanced serialization and deserialization techniques.