Search code examples
c++arraysoopfile-read

How to read coordinates from file in c++


I have got a file with coordinates of robot that contains X,Y of robot and degree of rotation.

0.2 0.3 150
1.7 3.2 -30
.....
8.7 5.2 -120

I have got also a file with different readings from robot sensors that look like

1.5 1.4 1.0. 1.5
4.5 3.4 8.3. 1.1
....
3.5 7.3 1.2. 12.5

And firstly I read coordinates of robot and then its sensor values and do some calculations. Is there any point to create a class for robot poses that will keep there variables?

double x,y; int degrees;

Because it is easier to make just an array of double[linesInFile][3] that will keep all values as double type that to create class of position and create array of position objects. And what is the best way to create that array if I dont know how many coordinates is in the file and I dont know the size of array till I read whole file? Can they be added dynamicalt or should I read how many lines is in the file firstly and then create new array?


Solution

  • Use a struct stored in a std::vector.

       struct Coord {
         double x;
         double y;
         double deg;
       };
    
       std::vector<Coord> myCoords;
    

    Now you can use push_back to add elements. std::vector will take care of memory management for you.