Reading .CSV file with colon into two dimensional array

I'm trying to read a .CSV file and then save the values into two dimensional array.

My problem is that I can't save the date into the columns and the temperature into the rows. I was able to save them as one line!

What should I change in order to do so?

string we[31][12];
int counter=0;
ifstream inFile("1.csv");
ofstream outFile("2.csv");
for (int i=0;i<31;++i) {
for (int j=0;j<12;++j){
getline(inFile,we[i][j],';');
counter=counter +1;
}
}
for (int i=0;i<31;++i) {
for (int j=0;j<12;++j){
outFile<< we[i][j]<<';';
counter=counter +1;
}
}

cout<<endl;
cout<<counter;

inFile.close();

Sample of the input file:

16.01.2017;-0.3
17.01.2017;-2.2

I tried to use the getline function as well. But it doesn't help as well.
Is there anyway to tell the code to save the value till the colon. Then read the next value and save into the next column.I really tried to do it by myself and read so many things.

for example, I want to save the date (16.01.2017) into one column and the temp (-0.3) into another column.

Now, the temperature is saved with the next date like this
" -0.3
17.01.2017"

Last edited on
All simple* CSV I/O looks like this:

Read:
1
2
3
4
5
6
7
8
9
10
  string s;
  while (getline( f, s ))
  {
    istringstream ss{ s };
    while (getline( ss, s, ';' ))
    {
      append s to current row
    }
    append current row to result
  }

Write:
1
2
3
4
5
6
7
8
9
  for each row in table
  {
    for each field in row
    {
      if (not first field in row) f << ';';
      f << field;
    }
    f << '\n';
  }

Since you are using a 2D array instead of a vector or some other resizable container, you must also be careful to terminate your read if you run out of space in either the rows or the table.

* simple == field separator cannot appear as a valid character in a field
Topic archived. No new replies allowed.