Reading Integers From A File

I'm having trouble getting integers from a file. If there is no space at the end of a file it works fine, but if there is a space after the last integer it grabs an extra junk number for some reason. This is my code so far

string fileName;
fstream file;
int fileArray[100];
int size;

cout << "Please enter the file name:";
cin >> fileName;
file.open(fileName);

if (!file)
{
cout << "File not found! Program terminating!!" << endl;
exit(1);
}

for (int i = 0; file.good(); i++)
{
file >> fileArray[i];
size++;
}
1
2
if (!(file >> fileArray[i]))
    //error reading or converting input 

Also, remember to ensure that i < 100.
Topic archived. No new replies allowed.