ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Read data from txt file on c++

asked 2015-04-15 04:41:12 -0600

charles gravatar image

updated 2022-01-22 16:16:30 -0600

Evgeny gravatar image

Hi everybody,

I have a package with a c++ source file (source.cpp) that reads data from a txt (filename.txt). I compile the package through "catkin_make package" and everything goes fine. What should I do to make the executable to find the txt file when I run "rosrun package executable"?

Edit: In the CMakeLists.txt I do "add_executable (executable source.cpp)". The filename.txt is in the same directory as source.cpp. How do I add the file name?

Thank you.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2015-04-15 07:15:45 -0600

Wolf gravatar image

Alternatively to @aak2166 answer you can use the ros::package API:

ros::package::getPath('PKG_NAME') should give you the package path, where you can append your relative resource path to get a full path.

edit flag offensive delete link more

answered 2015-04-15 07:11:03 -0600

aak2166 gravatar image

updated 2015-04-15 07:21:17 -0600

In your C++ source code you can make a call to the parameter server, where the path to that file is stored.

In your code, with Node Handle nh write:

std::string fpath;
nh.getParam("~private_param_name", fpath);

And now in your launch file add the line (I believe this is how it works, I've never done it this way, you might need to experiment a bit with this):

<node ...>
    <param name="private_param_name" command="'$(find your_package)/src/your_text_file.txt'"

Or if you want to run from the command line

rosrun your_package_name your_executable _private_param_name:="path_to_your_package/src/your_text_file.txt"

edit flag offensive delete link more

answered 2015-04-15 05:30:07 -0600

ROSkinect gravatar image

updated 2015-04-15 05:30:41 -0600

I don't know how you start your source.cpp ! but in your program you have to add the directory where your file is or at least its name if there are in the same directory.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-04-15 04:41:12 -0600

Seen: 3,866 times

Last updated: Apr 15 '15