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

Read data from txt file on c++

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

charles gravatar image

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

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
1

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

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
1

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

aak2166 gravatar image

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

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'"
</node>

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
0

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

ROSkinect gravatar image

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

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

Stats

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

Seen: 4,090 times

Last updated: Apr 15 '15