Reading a Matrix txt file and storing as an array

How about this? (KISS solution)

void LoadCities() {
  int x, y;
  ifstream in("Cities.txt");

  if (!in) {
    cout << "Cannot open file.\n";
    return;
  }

  for (y = 0; y < 15; y++) {
    for (x = 0; x < 15; x++) {
      in >> distances[x][y];
    }
  }

  in.close();
}

Works for me. Might not be that complex and perhaps isn’t very performant, but as long as you aren’t reading a 1000×1000 array, you won’t see any difference.

Leave a Comment