Ask Your Question

How do you save ROS tutorial subscriber data?

asked 2019-07-23 05:19:50 -0500

sw14928 gravatar image

In the "Writing a Simple Publisher and Subscriber (C++)" tutorial it states that in:

void chatterCallback(const std_msgs::String::ConstPtr& msg)
  ROS_INFO("I heard: [%s]", msg->data.c_str());

The message is passed in a boost shared_ptr, which means you can store it off if you want.

How exactly can I use this to store my data in a text file. I have no clue where to begin with this. I went through the boost shared_ptr documentation, but I do not understand it.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-07-23 05:55:04 -0500

Mehdi. gravatar image

What speaks against doing a normal string write?

#include <fstream>
#include <string>
#include <iostream>

void chatterCallback(const std_msgs::String::ConstPtr& msg)
    std::ofstream out("output.txt");
    out << msg->data.c_str();
edit flag offensive delete link more


Thanks for your advice - it works, but how can I make it print a list? cheers

sw14928 gravatar image sw14928  ( 2019-07-23 06:24:31 -0500 )edit

std::vector<std::string> str_vector; and then append msg->data.c_str() each time a message arrive then for loop on the vector and print the content

Mehdi. gravatar image Mehdi.  ( 2019-07-23 06:29:31 -0500 )edit

Sorry my C++ is not very good. Would that write my list to a vector, then how would I print that into my file iteratively? use append.msg?

sw14928 gravatar image sw14928  ( 2019-07-23 06:42:06 -0500 )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: 2019-07-23 05:19:50 -0500

Seen: 323 times

Last updated: Jul 23 '19