Ask Your Question

Print out the contents of a string using ROS_INFO

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

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 -0600

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 -0600 )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 -0600 )edit

Thanks for that!

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

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

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 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


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

Seen: 36,287 times

Last updated: Dec 09 '13