简体   繁体   中英

YAML::LoadFile(std::string const&) does not find file [yaml-cpp in ROS]

I'm trying to use data from a yaml file in a ROS(kinetic)/ c++ code so yaml-cpp seems like a good option for me. My code yields no errors but does not work properly:

It seems like the YAML::LoadFile function is not able to find my file since the following lines go to exception:

YAML::Node yamlnode_;     
try{
    yamlnode_= YAML::LoadFile("../yaml_file.yaml");
}
catch(std::exception &e){
    ROS_ERROR("Failed to load yaml file");
}

Including yaml-cpp via

#include <yaml-cpp/yaml.h>

seems to work since YAML:: functions are recognized afterwards.


The path ../yaml_file.yaml is set up correctly which I also checked in program via

#include "../yaml_file.yaml"

which yields parsing errors (as expected) which show me that the correct file was found (but obviously cannot be included).


The yaml_file.yaml is used successfully in multiple .xacro files.


Keep in mind that I am somewhat new to ROS and yaml-cpp; I'm looking forward to see your questions and answers

That including the YAML file using #include "../yaml_file.yaml" throws errors only indicates that that file resides somewhere in the parent directory of either the current directory or one of the directories in the include path the compiler uses.

When you use "../yaml_file.yaml" , for loading, then the file needs to be in the parent directory of the current directory at that point of execution. That is normally the parent directory of the directory from which you launch the program (which doesn't have to be the directory in which your program resides). But if your program changes the current directory (using chdir() ), and you have no idea where it might "be", you can just want to print the result of getcwd() to get your bearings.

You should use #include <unistd.h> for getcwd() , and it takes a pointer to a character buffer large enough to hold the path and as second parameter the ( size_t ) that buffer can hold. (With some implementations, you can pass NULL, and a buffer that is large enough is allocated, in that case use the return value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM