0

I have the task of reading an M x N matrix of numbers from a CSV file in C++. I have an idea of how to do this using std::vector (using a vector of vectors is my initial plan); however, I'm having trouble thinking of a way to read in the arbitrary number of columns. Is there an easy way to do this?

My initial plan is to read it line by line and manually find the commas separating the numbers, but again, there could be an arbitrary amount of them.

dandan78
  • 13,328
  • 13
  • 64
  • 78
Zcomp
  • 1

1 Answers1

0
  1. Read the first line.

  2. Split on , using something like strtok().

  3. Add each of the resulting substrings into your row vector.

  4. Add the row vector to your main vector.

  5. Repeat until all the rows in the CSV file have been processed.

Since vector handles its own memory and can contain an arbitrary number of items, you should be good to go.

dandan78
  • 13,328
  • 13
  • 64
  • 78