I was trying to read a pair of values line wise from a file but the integers i
and j
were not updating. Was my assignment of i
and j
wrong? I've found a way to get the code to work but I'm curious to know why the first while loop did not work.
Console output:
127 86
127 86
141 127
127 86
153 127
127 86
165 127
127 86
171 127
127 86
174 127
127 86
191 27
127 86
191 87
127 86
191 99
127 86
191 102
127 86
MWE:
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
void test()
{
ifstream inputfile;
inputfile.open("test.txt");
string line;
stringstream lineS;
int i, j;
while ( getline( inputfile, line ) )
{
lineS.str(line);
cout << lineS.str() << endl;
lineS >> i >> j;
cout << i << " " << j << endl << endl;
}
/* This works
while (!inputfile.eof()) {
inputfile >> i >> j;
cout << i << " " << j << endl << endl;
}*/
inputfile.close();
}
int main()
{
test();
return 0;
}
This is the text file test.txt:
127 86
141 127
153 127
165 127
171 127
174 127
191 27
191 87
191 99
191 102