8

I am very new to yaml-cpp. I tried the simplest program, but it failed and, and I couldn't find the answer from GitHub repository.

#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 the official YAML 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.

This is the error message:

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

Is it the library building problem or a YAML syntax problem or an issue with my API usage?

I am using yaml-cpp version 0.5.1.

Community
  • 1
  • 1
user001
  • 377
  • 2
  • 4
  • 8

3 Answers3

8

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!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for your explanation. `yaml-cpp` 0.5.1 supports Yaml1.2, so I supposed it could understand the `sample.yaml`. I will try some yaml file with 1.0 syntax later. – user001 Apr 16 '14 at 16:35
  • up vote also, thanks for providing useful information. – user001 Apr 17 '14 at 07:24
3

It appears that yaml-cpp can't find your file. Have you been able to load any file? Make sure that the file is in the working directory of your program, and for sanity's sake, make sure that you can load a very simple file first.

Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
0

Your yaml file needs to be where your endstate lies, based on the path you have given.

hal9000
  • 201
  • 5
  • 25