Loading .dat file into 2D vector

I have a .dat file with one long row of integers separated by a space. I need to load this file into a 2D vector of specific rows and columns. Any pointers on where to start would be greatly appreciated. So far, all my code does is successfully open the file.

closed account (48T7M4Gy)
Best place to start with this is to write some pseudocode
you start building a 2d array to store your values
1
2
3
4

#include <array>
// ... some stuff here
typedef std::array<std::array<int>, COL>, ROW> yourMatrix;


After that you write a nested cycle to read numbers from the file

1
2
3
4
5
6
7
8
yourMatrix aMatrix;
if(inputStream.is_open()){
  for(int i = 0; i< COL; i++){
    for(int j = 0; j< ROW; j++){
      inputStream >> aMatrix[j][i];
    }
  }
}


This code assume you already know the number of rows (ROW) and columns (COL) of your matrix.

EDIT:: I read in numbers columnwise, which is the FORTRAN standard. In c++ I suppose is better to read the values row by row, but I am not 100% sure about that (in c I am sure).
Last edited on
Topic archived. No new replies allowed.