Parse YAML Files in C++

后端 未结 4 1356
醉话见心
醉话见心 2020-12-07 18:28

I want a simple tutorial to show me to load a yaml file and parse the data. Expat style would be great but any solution that actually shows me the data in some form would be

4条回答
  •  日久生厌
    2020-12-07 18:46

    Try yaml-cpp (as suggested by this question) for a C++ parser.

    Disclosure: I'm the author.

    Example syntax (from the Tutorial):

    YAML::Node config = YAML::LoadFile("config.yaml");
    
    if (config["lastLogin"]) {
      std::cout << "Last logged in: " << config["lastLogin"].as() << "\n";
    }
    
    const std::string username = config["username"].as();
    const std::string password = config["password"].as();
    login(username, password);
    config["lastLogin"] = getCurrentDateTime();
    
    std::ofstream fout("config.yaml");
    fout << config;
    

提交回复
热议问题