I'm trying to read a file containing several variable length lists. Each list is on one line and I read it into a vector. But the last element on each line is getting stored into the vector twice.
I've coded it along the following lines:
ifstream file;
file.open("myfile.txt", ifstream::in);
string line;
while(!file.eof())
{
getline(file, line);
stringstream buffer(line);
vector<int> temp;
while (!buffer.eof())
{
buffer >> num;
temp.push_back(num);
}
for(vector<int>::iterator i = temp.begin(); i != temp.end(); ++i)
cout << *i << ' ';
}
Lines of input consist of tab separated integers. There is also a tab after the last element of each line. For a line like
1 3 4 2
The expected output is
1 3 4 2
The output I'm getting is
1 3 4 2 2