#include <fstream>
#include <vector>
#include <iostream>
usingnamespace std;
int main()
{
vector<vector<int>> matVec;// 2d vector for storing all data read from file
vector<int> lineVec;// for the entries on each line
ifstream infile("dataIn.txt");
if(infile)
{
int val = 0;
while(!infile.eof())
{
infile >> val;
lineVec.push_back(val);
if( infile.peek() == '\n' )// last entry on the line has been read
{
matVec.push_back(lineVec);// push all of the lines entries at once
lineVec.clear();// prepare to read the next line
}
}
infile.close();
// output the matrix contents to test the file read
for(size_t n=0; n<matVec.size(); ++n)
{
for(size_t k=0; k<matVec[n].size(); ++k)
cout << matVec[n][k] << " ";
cout << endl;
}
}
else
cout << "file was not opened." << endl;
cout << endl;
return 0;
}
EDIT: I just noticed that the last line doesn't get stored unless it ends with a newline so, make sure a newline appears at the end of the file or fix the program so it works regardless.