c++ - How to use ifstream to input a file into a 2D array? -
this question has answer here:
i'm struggling on overloading ifstream
operator input file in matrix form , creating 2d array. 3x3 matrix. small part of assignment without whole assignment quite pointless.
file example:
1 2 3 4 5 6 7 8 6
i have done way...
#include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; int main() { int data[3][3]; int = 0; int j = 0; ifstream in(filename); std::string line; std::string temp; while(std::getline(in, line)) { std::istringstream iss(line); // parse each line using input string stream j = 0; while(std::getline(iss,temp,' ')) { data[i][j] = std::stoi(temp); j++; } i++; } return 0; }
Comments
Post a Comment