How to load YAML file via yaml-cpp?

user001 picture user001 · Apr 16, 2014 · Viewed 13.5k times · Source

I am very new on yaml-cpp, I tried the simplest program but failed and also didn't find answer from author's google/github website.

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

using namespace std;

int main()
{
    YAML::Node config = YAML::LoadFile("sample.yaml");
    return 0;
}

sample.yaml sample from YAML official website

--- !clarkevans.com/^invoice
invoice: 34843
date   : 2001-01-23
bill-to: &id001
    given  : Chris
    family : Dumars
    address:
        lines: |
            458 Walkman Dr.
            Suite #292
        city    : Royal Oak
        state   : MI
        postal  : 48046
ship-to: *id001
product:
    - sku         : BL394D
      quantity    : 4
      description : Basketball
      price       : 450.00
    - sku         : BL4438H
      quantity    : 1
      description : Super Hoop
      price       : 2392.00
tax  : 251.42
total: 4443.52
comments: >
    Late afternoon is best.
    Backup contact is Nancy
    Billsmer @ 338-4338.

The error message is given like this:

libc++abi.dylib: terminating with uncaught exception of type YAML::BadFile: yaml-cpp: error at line 0, column 0: bad file

May I know what's the problem? Is it the library building problem or YAML syntax problem or API using problem?

Development environment

MacOSX10.9 c++11 IDE:QtCreator3.0.1 yaml-cpp 0.5.1


[Solved]
I did stupid mistake that I loaded wrong path of sample.yaml.

Answer

David Heffernan picture David Heffernan · Apr 16, 2014

I think that the YAML parser in the version of yaml-cpp that you are using is just not advanced enough to able to handle the first line of your sample file. The sample file is trying to illustrate quite a few of the more advanced YAML features. It seems that your parser cannot handle them all. I suggest that you start with a simpler example file.

I checked your file with three online validators with the following results:

No doubt the file is valid YAML but that does not mean that all extant parsers can parse it!