How to Read YAML Files with C++

🚀 Simple, fast and practical!


How to Read YAML Files with C++


yaml-cpp is a parser for YAML with C++.

With it you can read YAML files quickly through C++ code.


Installation

To install, just clone with git, compile and install with CMake, run the commands below:

git clone https://github.com/jbeder/yaml-cpp
cd yaml-cpp
cmake -B build .
cmake --build build
sudo cmake --install build
See here the files that are installed
-- Install configuration: ""
-- Installing: /usr/local/lib/libyaml-cpp.a
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/yaml-cpp
-- Installing: /usr/local/include/yaml-cpp/emitterstyle.h
-- Installing: /usr/local/include/yaml-cpp/eventhandler.h
-- Installing: /usr/local/include/yaml-cpp/binary.h
-- Installing: /usr/local/include/yaml-cpp/emitterdef.h
-- Installing: /usr/local/include/yaml-cpp/yaml.h
-- Installing: /usr/local/include/yaml-cpp/null.h
-- Installing: /usr/local/include/yaml-cpp/contrib
-- Installing: /usr/local/include/yaml-cpp/contrib/anchordict.h
-- Installing: /usr/local/include/yaml-cpp/contrib/graphbuilder.h
-- Installing: /usr/local/include/yaml-cpp/ostream_wrapper.h
-- Installing: /usr/local/include/yaml-cpp/anchor.h
-- Installing: /usr/local/include/yaml-cpp/parser.h
-- Installing: /usr/local/include/yaml-cpp/emitfromevents.h
-- Installing: /usr/local/include/yaml-cpp/emittermanip.h
-- Installing: /usr/local/include/yaml-cpp/traits.h
-- Installing: /usr/local/include/yaml-cpp/exceptions.h
-- Installing: /usr/local/include/yaml-cpp/fptostring.h
-- Installing: /usr/local/include/yaml-cpp/node
-- Installing: /usr/local/include/yaml-cpp/node/parse.h
-- Installing: /usr/local/include/yaml-cpp/node/type.h
-- Installing: /usr/local/include/yaml-cpp/node/ptr.h
-- Installing: /usr/local/include/yaml-cpp/node/node.h
-- Installing: /usr/local/include/yaml-cpp/node/convert.h
-- Installing: /usr/local/include/yaml-cpp/node/impl.h
-- Installing: /usr/local/include/yaml-cpp/node/emit.h
-- Installing: /usr/local/include/yaml-cpp/node/iterator.h
-- Installing: /usr/local/include/yaml-cpp/node/detail
-- Installing: /usr/local/include/yaml-cpp/node/detail/memory.h
-- Installing: /usr/local/include/yaml-cpp/node/detail/node.h
-- Installing: /usr/local/include/yaml-cpp/node/detail/impl.h
-- Installing: /usr/local/include/yaml-cpp/node/detail/node_data.h
-- Installing: /usr/local/include/yaml-cpp/node/detail/node_iterator.h
-- Installing: /usr/local/include/yaml-cpp/node/detail/iterator_fwd.h
-- Installing: /usr/local/include/yaml-cpp/node/detail/iterator.h
-- Installing: /usr/local/include/yaml-cpp/node/detail/node_ref.h
-- Installing: /usr/local/include/yaml-cpp/dll.h
-- Installing: /usr/local/include/yaml-cpp/stlemitter.h
-- Installing: /usr/local/include/yaml-cpp/noexcept.h
-- Installing: /usr/local/include/yaml-cpp/depthguard.h
-- Installing: /usr/local/include/yaml-cpp/mark.h
-- Installing: /usr/local/include/yaml-cpp/emitter.h
-- Installing: /usr/local/lib/cmake/yaml-cpp/yaml-cpp-targets.cmake
-- Installing: /usr/local/lib/cmake/yaml-cpp/yaml-cpp-targets-noconfig.cmake
-- Installing: /usr/local/lib/cmake/yaml-cpp/yaml-cpp-config.cmake
-- Installing: /usr/local/lib/cmake/yaml-cpp/yaml-cpp-config-version.cmake
-- Installing: /usr/local/lib/pkgconfig/yaml-cpp.pc

Basic example

Let’s suppose you have this example file of the YAML type: example.yaml:

person:
    name: Marcos
    last name: Oliveira
    age: 39
    profession: Programmer
    height: 1.73

And here is the C++ code that will do the parsing: main.cpp

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

int main(){
    YAML::Node doc = YAML::LoadFile("./example.yaml");

    YAML::Node person = doc["person"];

    std::string name = person["name"].as<std::string>();
    std::string last name = person["last name"].as<std::string>();
    int age = person["age"].as<int>();
    std::string profession = person["profession"].as<std::string>();
    double height = person["height"].as<double>();

    std::println("First name: {}", name);
    std::println("Last name: {}", last name);
    std::println("Age: {}", age);
    std::println("Profession: {}", profession);
    std::println("Height: {}", height);

    return EXIT_SUCCESS;
}

Remember that in this example I used std::println instead of iostream.

Compile using the flag for yaml-cpp:

g++ main.cpp -lyaml-cpp

And after running the binary the output will be:

First name: Marcos
Last name: Oliveira
Age: 39
Profession: Programmer
Height: 1.73

Easy, right?! For more information access the repository and the examples for using the API


cppdaily cpp


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles