reading numbers from a file

..when you don't know how many they are. say you use the while loop to print out numbers in some file. but you don't know the test expression. the numbers could be anywhere from say 10 to 500. any specific loop or function?
Loop until the file stream is not good any more
eg:
1
2
3
4
5
6
ifstream myfile;
//...
while ( myfile )
{
    // read number
}
or
1
2
3
4
5
6
7
8
9
ifstream myfile;
//...
while ( true )
{
    // read number
    if ( !myfile )
        break; // exit from the loop
    //...
}
Shouldn't you be reading before checking?

1
2
3
4
5
ifstream myfile;

do {
  // read number
} while (myfile);


the post below me is awesome
Last edited on
He is reading before checking. The break gets him out of the loop before he tries to do anything with the number read.
Oh, my bad.
Topic archived. No new replies allowed.