Trouble writing to file
-
Hi
I got a problem with saving some data to a file on my system.
As the picture shows i got a vector of float in which i store some data. This vector is then stored in another vector which is declared in my header file:
I do a n++ to keep track of the number of vectors that is stored in the vector rtlData.
When i press a button called saveRTLData i write the vector rtlData to a txt or preferably a csv file. And here is where my program crashes.
To begin with i wrote the whole path "/home/user/.../rtlData.csv" in my code and when i pressed the button i got the message "Stream not open". Then i tried changing the path so it's just rtlData.csv or rtlData.txt as the picture shows, which resultet in the following error:
[rtl_temp_node-3] process has died [pid 15572, exit code -11, cmd /home/christian/catkin_ws/devel/lib/rtl_temp/rtl_temp_node __name:=rtl_temp_node __log:=/home/christian/.ros/log/4c8dc630-2133-11e8-b647-b8ee65ea785f/rtl_temp_node-3.log].
log file: /home/christian/.ros/log/4c8dc630-2133-11e8-b647-b8ee65ea785f/rtl_temp_node-3*.logDoes anyone here know where my mistake is?
-
@Christianvs
It helps if you paste actual textual code, not screenshots...for (int i = 0; i <= n; i++)
If
n
is the number of rows ("vectors"), don't you meani < n
, as is standard in all C-type languages? -
@Christianvs
Yes, get used to the fact that in C-type an array withn
elements is always index from0
ton - 1
, inclusive. Trying to access then
-th element will crash (if you're lucky) or exhibit completely unknown behaviour (if you're unlucky). -
@mranger90 said in Trouble writing to file:
Or, since you're using a container, use the appropriate iterators.
Which would mean:
for(const auto& singleRow : rtlData){ for(const auto& singleVal : singleRow){ rtlDataStream << singleVal; } rtlDataStream << std::endl; }
-
Thanks for all the replies folks!
I feel quite stupid for not seeing that .size() actually solves the entire problem, but I am still quite green when it comes to programming.
Thanks for the tip with iterators @mranger90 @VRonin ! I'll look into that