Code to take input csv/txt, sort, output csv/txt

The data file provided consists of trajectories of vehicles for 10 min (600s) with various fields such as
Time (s), Vehicle number, Longitudinal position (m), Lateral position (m), and Vehicle type. However,
the data that is logged-in in the datafile provided is according to the sequence of time and not with
respect to the vehicle number. For example, the data corresponding to vehicle number 2 is logged in
at various places starting with row no. 15, and ending at row no. 1612 (see the provided data file).
Ideally, we would like to see the trajectory of any given vehicle logged in continuously. For this to
happen, we need to process the data in such a way that all the data corresponding to a given vehicle
appears at one place. Write a code in CPP to process the data and create an output file in the format
described above.


Time (s) Veh # Long. Pos (m) Lat. Pos (m) Veh. Type
0.2 1 0.88 0.5 Truck
0.3 1 2.45 0.5 Truck
0.4 1 4.02 0.5 Truck
0.5 1 5.59 0.5 Truck
0.6 1 7.17 0.5 Truck
0.7 1 8.74 0.5 Truck
0.8 1 10.32 0.5 Truck
0.9 1 11.9 0.5 Truck
1 1 13.48 0.5 Truck
1.1 1 15.06 0.5 Truck
1.2 1 16.65 0.5 Truck
1.3 1 18.24 0.5 Truck
1.4 1 19.82 0.5 Truck
1.4 2 1 0.5 Bike
1.5 1 21.41 0.5 Truck
1.5 2 4 0.5 Bike
1.6 1 23.01 0.5 Truck
1.6 2 7 0.5 Bike
1.7 1 24.6 0.5 Truck
1.7 2 10.01 0.5 Bike
1.8 1 26.2 0.5 Truck
1.8 2 13.02 0.5 Bike
1.9 1 27.79 0.5 Truck
1.9 2 16.02 0.5 Bike
2 1 29.39 0.5 Truck
2 2 19.04 0.5 Bike
2.1 1 30.99 0.5 Truck
2.1 2 22.05 0.5 Bike
2.2 1 32.6 0.5 Truck
Last edited on
captain obvious wonders why data that is already readable by a spreadsheet that can already sort the data any which way is being manhandled by c++?

That aside, just read the strings from the original file into a vector of some sort of class or struct that has the 5 fields, sort it on the desired value (s), and write it out with commas between the fields into a text file with .csv extension. So the first thing to do is make your container for the fields. Can you do that?

if it were ME doing this 'for real' (not homework) I would probably write each truck to its own sheet.

Last edited on
Topic archived. No new replies allowed.