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

Print out the contents of a string using ROS_INFO

asked 2013-12-09 10:38:59 -0500

JP gravatar image

Hi all,

I have a line such as:

   std_msgs::String s; = c.c_str(); //same data as above.

What I would like to do is look at the contents of s that is passed into the method publish(). I tried something like:

ROS_INFO("(2) (%s) \n",s.c_str());

But that failed at compile time. Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-12-09 10:48:01 -0500

std_msgs::String is not the same thing as std::string and cannot be treated as such; it is a message type that has a std::string member variable called data. Thus, the correct way to output the message string is


edit flag offensive delete link more


Thank you!

JP gravatar image JP  ( 2013-12-09 11:51:13 -0500 )edit

For additional clarification, using ROS_INFO_STREAM uses the ostream operator implementation (operator<<) that rosmsg generates for each message type. The ostream operator is similar to the output you would see from rostopic echo.

mjcarroll gravatar image mjcarroll  ( 2013-12-12 03:05:33 -0500 )edit

Thanks for that!

JP gravatar image JP  ( 2013-12-12 09:52:31 -0500 )edit

For some reason c_str() didn't work for me so I used data() instead: ROS_INFO("frame: %s",; or in OP case ROS_INFO("%s\n",;

Combinacijus gravatar image Combinacijus  ( 2022-12-14 08:12:07 -0500 )edit

answered 2013-12-09 10:42:32 -0500

mjcarroll gravatar image

I tend to use ROS_INFO_STREAM when I'm doing simple debugging.

You should be able to do:



ROS_INFO_STREAM("Msg: " << s);

edit flag offensive delete link more



This is useful, thanks!

JP gravatar image JP  ( 2013-12-09 11:51:33 -0500 )edit

I tried this for moveit_msgs/RobotTrajectory.msg and it worked fine. And I think it can print any type of ROS message. Thanks!

askkvn gravatar image askkvn  ( 2021-06-18 02:37:08 -0500 )edit

Question Tools

1 follower


Asked: 2013-12-09 10:38:59 -0500

Seen: 49,187 times

Last updated: Dec 09 '13