reading numbers from a file
Oct 28, 2010 at 11:59pm UTC
..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?
Oct 29, 2010 at 9:58am UTC
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
//...
}
Nov 2, 2010 at 1:56am UTC
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 Nov 2, 2010 at 4:10am UTC
Nov 2, 2010 at 2:47am UTC
He is reading before checking. The break gets him out of the loop before he tries to do anything with the number read.
Nov 2, 2010 at 4:09am UTC
Oh, my bad.
Topic archived. No new replies allowed.