EOF is added to my vector when reading from file?

Oct 18, 2016 at 3:49am
So what I am trying to do is read data from a text file, and add the variables to MyClass then push it to the end of myVector.

The problem is that the EOF is read and added to my vector before breaking the while loop, resulting in garbage at the last index of the vector. I tried checking for the EOF mid loop and post loop, but then the issue is that the last good line of my file is not added to the vector.

1
2
3
4
5
 	while (!inFile.eof()) {

			inFile >> a >> b >> c >> d >> e;
			myVector.push_back(MyClass(a, b, c, d, e));
	}


The text file I am reading from is TAB and new line separated and looks something like below:

1
2
3
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
Oct 18, 2016 at 3:56am
1
2
3
4
5
6
 	// while (!inFile.eof()) {
        while( inFile >> a >> b >> c >> d >> e ) { // canonical: check for input failue *after* attempted input

			// inFile >> a >> b >> c >> d >> e;
			myVector.push_back(MyClass(a, b, c, d, e));
	}
Oct 18, 2016 at 4:22am
Thanks @JLBorges!
Topic archived. No new replies allowed.