fork download
  1. #include <fstream>
  2. #include <vector>
  3.  
  4. constexpr int COLUMNS_AMOUNT = 10; //amount of columns in the textfile
  5.  
  6. int main(int argc, char** argv)
  7. {
  8. std::ifstream file_stream("jy.txt"); //create filestream to file
  9.  
  10. std::vector<std::vector<int>>* matrix; //pointer to multidimensional vector
  11. matrix = new std::vector<std::vector<int>>(); //allocate new vector on the heap
  12.  
  13. int index=0, tmp;
  14.  
  15. while (file_stream.good()) //while data in the filestream
  16. {
  17. for (int i=0; i<COLUMNS_AMOUNT; i++) //for every row
  18. {
  19. if (file_stream.eof()) //check for end of file
  20. break;
  21.  
  22. file_stream >> tmp; //fetch the next value
  23. (*matrix)[index][i] = tmp; //store the next value
  24. }
  25.  
  26. ++index; //increment row-index
  27. }
  28.  
  29. //do whatever you want with the matrix
  30.  
  31. file_stream.close();
  32. delete matrix;
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty