Problem reading numbers from file
Dec 4, 2011 at 7:59pm UTC
Hello,
I am trying to read some numbers from a file and outputting the numbers on the screen. But the last number in the file gets outputted twice. Here's the code I have so far
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 <fstream>
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
int main() {
const char * filename = "test.txt" ;
std::ifstream inFile(filename);
if (!inFile) {
cout << endl << "Failed to open file " << filename;
return 1;
}
int n = 0;
while (!inFile.eof()) {
inFile >> n;
cout << std::setw(10) << n;
}
cout << endl;
inFile.close();
return 0;
}
Anyone got any advice on how to fix this please?
Thanks.
Dec 4, 2011 at 8:03pm UTC
Dec 5, 2011 at 5:05am UTC
Thanks a lot for both the post and the link! They were very helpful :)
Topic archived. No new replies allowed.