0
$\begingroup$

Rosanswers logo

Hi, Can somebody explain me the following line of code line by line ? The code has been taken from ros tutorials.

  std_msgs::String msg;
  std::stringstream ss;
 ss << "hello world " << count;
  msg.data = ss.str();
   ROS_INFO("%s", msg.data.c_str());

Here is the link to the code http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29


Originally posted by SaurabhR on ROS Answers with karma: 5 on 2016-08-02

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

It will simply create a ROS message, fill it with a text and print it. Here's the line by line explanation:

std_msgs::String msg;  // create a standard message object of type String (used to send messages between ROS services / nodes / programs)

std::stringstream ss;  // create a stringstream object which will store our text and take care of other operations on strings 

ss << "hello world " << count;  // fill the stringsream object with text "Hello world X". X represent the iteration number which was previously stored in "count" variable 

msg.data = ss.str();  // fill the data field of our ROS message with the text we previously stored in the stringstream object (you can see the stringstream object as a buffer where we store text) 

ROS_INFO("%s", msg.data.c_str()); // print the content of the ROS message in the console and save it in the log file

Originally posted by Horse-man with karma: 229 on 2016-08-03

This answer was ACCEPTED on the original site

Post score: 2

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.