Lets say I have txt.file as
7 8 9
10 11 1
14 15 1
I want to keep the line and then put them in a nested vector. I have done this:
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
vector<vector<int> > data;
vector<int> temp;
string file;
cout << "Enter a file: ";
cin >> file;
ifstream infile(file);
if (!infile.good()) {
cout << "Enter a valid file: " << endl;
return 1;
}
string read;
while (!infile.eof()) {
getline(infile, read);
istringstream iss(read);
copy(istream_iterator<int>(iss), istream_iterator<int>(), back_inserter(temp));
data.push_back(temp);
}
for (int i = 0; i < data.size(); i++) {
for (int j = 0; j < data[i].size(); j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
Now the output I get is like this:
7 8 9
7 8 9 10 11 1
7 8 9 10 11 1 14 15 1
I am not sure why previous content is repeating. Any help would be appreciated. Thank you!