read from a file, got an extra line

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
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
int main() {
	ifstream inFile;

	vector<int> arr;
	inFile.open("myFile.txt");
	if(inFile.is_open()) {		
	     while( inFile.good() ) {
                int temp = 0;
		inFile >> temp;
		arr.push_back(temp);
	     }
	}  else {
		cout << "Unable to open file.\n";
                return 0;
	}

	inFile.close();
	for(size_t i = 0; i < arr.size(); ++i) {
		cout << arr.at(i) << ' ';
	}	
                cout << endl;


	cout << "Array size is " << arr.size() << endl;

	return 0;
}


myFile.txt:
6 9 16 52

first I use the vim and g++ command to compile it:
g++ -o ......

but the output is
6 9 12 52 0 
Array size is 5

what the hell is the 0?
last time I compile it, it output
6 9 12 52 52
, repeat the last number

but I recompile it in the Eclipse, it works out correctly.
 6 9 12 52 

why? what's the problem?


It's so strange that if I use g++ command compile an cpp file in Eclipse , the exectable file works correctly, why?
if I use vim and g++ cimpile, it doesn't work right.
Does Eclipse do something to the cpp file?
Last edited on
I change the code to this, in g++ an Eclipse all works right, why?
1
2
3
4
int temp = 0;
while( inFile >> temp ) {
	arr.push_back(temp);
}
Last edited on
Topic archived. No new replies allowed.