0
$\begingroup$

Rosanswers logo

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.


Originally posted by sw14928 on ROS Answers with karma: 3 on 2019-07-23

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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();
    out.close();
}

Originally posted by Mehdi. with karma: 3339 on 2019-07-23

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by sw14928 on 2019-07-23:
Thanks for your advice - it works, but how can I make it print a list? cheers

Comment by Mehdi. on 2019-07-23:
std::vectorstd::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

Comment by sw14928 on 2019-07-23:
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?

$\endgroup$

Your Answer

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