Posted by Milly Pan on July 28, 1999 at 01:29:55:
In Reply to: Re: Easy Question posted by mm on July 27, 1999 at 17:52:24:
: : In the OutputDir function that was give to us ...
: : void OutputDir ( char *dir, char level )
: : {
: : char s[2048];
: : int state;
: : DirectoryReader A (dir);
: : while ((state = A.GetName (s, sizeof (s))))
: : {
: : if ((strcmp (s, ".") != 0) && (strcmp (s, "..") != 0))
: : {
: : for ( int i=0; i<=level; i++ )
: : cout << "---";
: : cout << s << "\n";
: : if (state == 2)
: : OutputDir (s, level+1);
: : }
: : }
: : }
: : How does the while statement work?
: : while ((state = A.GetName (s, sizeof (s))))
: : Wouldn't that *always* be true?
: : I know I'm missing something simple ...
: The return value from GetName is either 0 (for no more files), 1 (for a filename), or 2 (for a dir name). When GetName returns 0 (i.e., when there are no more directories or files), the while condition is false and the loop terminates.
Of course, the "=" should actually be a "==", right? Or is this "while" condition doing something "different"?
No wait, I see. The condition is actually "while (state)" but what it's doing is saying in one line (and so the value doesn't have to be "stored" temporarily), state = GetName. THEN while (state), do such-and-such. Right?