Oct 15, 2009 at 7:22am
Why all these checks on ifile
? A single check while (ifile)
will do.
Oct 15, 2009 at 7:47am
ok... that reduced count by one. but, still the last one is printed twice...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
int main()
{
string line;
ifstream ifile("numbers.txt");
int num;
while(ifile)
{
clock_t start = clock();
getline(ifile, line);
istringstream ss(line);
while(ss.good()) { ss >> num; cout << num << endl; }
}
ifile.close();
}
|
Last edited on Oct 15, 2009 at 7:48am
Oct 15, 2009 at 8:45am
while(ss.good()) { ss >> num; cout << num << endl; }
Try change it from a while() loop to if() -
if(ss.good()) { ss >> num; cout << num << endl; }
Oct 15, 2009 at 11:33am
The problem is yours fails if there's more stuff in the stream, but no more numbers, like a space at the end of the line.