简体   繁体   English

YAML :: LoadFile(std :: string const&)找不到文件[ROS中的Yaml-cpp]

[英]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. 我正在尝试使用ROS(动态)/ c ++代码中的yaml文件中的数据,因此yaml-cpp对我来说似乎是一个不错的选择。 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::LoadFile函数无法找到我的文件,因为以下几行进入异常:

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 包括yaml-cpp通过

#include <yaml-cpp/yaml.h>

seems to work since YAML:: functions are recognized afterwards. 自从YAML ::函数之后被识别以来,似乎可以正常工作。


The path ../yaml_file.yaml is set up correctly which I also checked in program via 路径../yaml_file.yaml设置正确,我也通过以下方式检入程序

#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. yaml_file.yaml已成功在多个.xacro文件中使用。


Keep in mind that I am somewhat new to ROS and yaml-cpp; 请记住,我是ROS和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. 使用#include "../yaml_file.yaml"包含YAML文件会引发错误,仅表明该文件位于当前目录的父目录中,或者位于编译器使用的include路径中的目录之一中。

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. 当使用"../yaml_file.yaml"进行加载时,该文件需要在执行时位于当前目录的父目录中。 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. 但是,如果您的程序更改了当前目录(使用chdir() ),并且您不知道它可能在“哪里”,那么您只想打印getcwd()的结果来了解您的情况。

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. 您应该对getcwd()使用#include <unistd.h> ,它需要一个指向足以容纳路径的字符缓冲区的指针,并将该缓冲区可以容纳的( size_t )作为第二个参数。 (With some implementations, you can pass NULL, and a buffer that is large enough is allocated, in that case use the return value) (在某些实现中,您可以传递NULL,并分配足够大的缓冲区,在这种情况下,请使用返回值)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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