Reading from text file til .eof()

I am attempting to read in unknown number of scores from a text file and assign each score to an element in the int scores[100] array with the following code:

1
2
3
4
5
6
7
8
9
void getData ( int score[], int& len, fstream& data )
{
	// continue to read data until end of file reached
	while ( !data.eof() )
	{
		data >> score[len];
		len++;
	}
}


After assigning all scores to the array, I perform a few other functions. Each time I read in a score, I want to increase my len variable to identify how many scores were read in. The problem is there are exactly 67 scores in the text file, so that means the assignment will end at scores[66]. However, when I output this info it seems it has counted 68 scores and I have no idea how that's possible. I do see a problem with the way I'm incrementing len++. Upon reaching eof, I'm increasing len one more time to 67 which is false, but that doesn't explain how its counting to 68. If anyone has any feedback it would be greatly appreciated!

Thanks,
Return 0;
You're incrementing len correctly. When you read the last score into score[66], there'll be 67 scores. Thus, len should equal 67 at the end.

Could there be an empty line?
Suppose the file looks like this:

1<newline>
2<newline>
3<newline>
4<newline>
5<newline>
6<newline>

This will make len==7, but this:

1<newline>
2<newline>
3<newline>
4<newline>
5<newline>
6

will make len==6.
I don't believe it... that's hilarious, I swear I checked for an extra line 100 times... it's time for bed. Thanks for your help helios!
Topic archived. No new replies allowed.