Problem reading numbers from file

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.
Change your loop to
1
2
3
while(inFile >> n) {
	cout << std::setw(10) << n;
}

Read here for why http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5
Read here for why http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

Thanks a lot for both the post and the link! They were very helpful :)
Topic archived. No new replies allowed.