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

Write to a file from a C++ ROS node?

asked 2011-10-22 11:21:16 -0500

gavinmachine gravatar image

updated 2014-01-28 17:10:37 -0500

ngrennan gravatar image

Hello,

Is there a way to write to a file from the C++ source of a ROS node? I know that I can write directly to a .bag file, however I would like to be able to write to my own file formats (i.e. directly to a CSV rather than having to export a CSV from rxbag, etc).

I tried this from within the C++ of a node:

ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();

With the above code, the program executes just fine but I can't find "example.txt" anywhere on the file system. Does ROS just ignore this operation, or put the file somewhere I can't seem to find?

Thanks for the help!

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
11

answered 2011-10-22 14:11:49 -0500

thericcer gravatar image

You should put in the whole path for where you want the file to be written.

myfile.open("/home/user/example.txt")

That way the file is always created in /home/user/ and not the directory ros runs the program from.

edit flag offensive delete link more

Comments

I was writing some STL files (output from nodes that worked with PCL and converted point clouds to meshes). I've noticed that when you don't give the full path but just the file name (example: my_mesh.stl) the file is written into .ros (the hidden folder inside the user's /home. Weird...

rbaleksandar gravatar image rbaleksandar  ( 2015-09-27 23:36:30 -0500 )edit

like the comment above, i found my files in the hidden folder .ros,

xingxiajie gravatar image xingxiajie  ( 2019-05-11 19:55:45 -0500 )edit

is it possible to instead of passing the whole path to use something like $(find package)/data/example.txt?

rezenders gravatar image rezenders  ( 2019-12-12 13:28:40 -0500 )edit
6

answered 2011-10-22 14:23:07 -0500

Thomas D gravatar image

updated 2011-10-23 04:40:49 -0500

I just tried your example code but changed it to:

#include <fstream>
using namespace std;
.
.
.
ofstream myfile("example.txt", ios::out | ios::binary);
myfile << "Writing this to a file.\n";
myfile.close();

Edit: Cleaning up thanks to @joq's comment.

Using rosrun this created and wrote to "example.txt" in the directory that I ran the node from. I tried running from several different directories, and each time the file "example.txt" showed up in the directory I was in, and had nothing to do with the location of the node binary.

Using roslaunch this created and wrote "example.txt" in ~/.ros no matter which directory I run the roslaunch command from.

edit flag offensive delete link more

Comments

1
That works with `rosrun` but not `roslaunch`.
joq gravatar image joq  ( 2011-10-22 15:49:23 -0500 )edit
2

Actually it shows up in whatever ROS_HOME is set to and defaults to ~/.ros.

dornhege gravatar image dornhege  ( 2013-04-03 07:13:03 -0500 )edit
-4

answered 2013-04-03 07:03:31 -0500

void insert_node(char* name,int roll_no ,char* sems,char* major,float gpa) { fstream myfile; myfile.open("myfile.txt", ios::out|ios::binary|ios::app); Node* newptr = new Node(/name,roll_no,sems,major,gpa/); if(firstptr==NULL && lastptr==NULL) { firstptr = newptr; lastptr = newptr; } else if((newptr->getname()>firstptr->getname())&&(newptr->getname()>lastptr->getname())) { lastptr->nextptr = newptr; newptr->nextptr = NULL; lastptr = newptr; } else if(newptr->getname()<firstptr->getname()) { newptr->nextptr = firstptr; firstptr = newptr; } else { Node* tempptr; tempptr = firstptr; while(newptr->getname()> tempptr->nextptr->getname()) { tempptr = tempptr->nextptr; } newptr->nextptr = tempptr->nextptr; tempptr->nextptr = newptr; }

                   myfile.write((Node*)&newptr, sizeof(newptr)); //i have error on thiis line but if i put char* instead of Node* then i got a different type of output in my file plzz anyone help me.. :(

                   myfile.close();
                  }
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2011-10-22 11:21:16 -0500

Seen: 14,273 times

Last updated: Apr 03 '13