Using ifstream variable in while loop condition

I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	const int ARRAY_SIZE = 100;
	int numbers[ARRAY_SIZE];
	int count = 0;
	ifstream inputFile;

	inputFile.open("numbers.txt");

	while ( count < ARRAY_SIZE && inputFile >> numbers[count] )
		count++;

	inputFile.close();
	return 0;
}


I don't understand exactly what inputFile >> numbers[count] is doing. I understand its reading in the numbers from the file and assigning them to each element in the array, but I don't really understand what its doing in the condition list. If ARRAY_SIZE is the maximum iteration of the loop, what's the purpose of the inputFile condition? Any help will be greatly appreciated.

Thanks,
Return 0;
Because that checks whether the number has been successfully read from the stream

eg: if the file was like this:
1 2 3 4 5 6 not_a_number 7 8 9 ...
the numbers are read until "not_a_number" is reached, after that error the loop is terminated
Last edited on
Ah I see, that makes sense. Thank you.
Topic archived. No new replies allowed.