Click to See Complete Forum and Search --> : im learning loops


Brad.G
June 12th, 2006, 06:17 AM
im learning loops and i just have a couple of questions so im perfectly clear on whats going on.

ive read a tutorial on for, while & do, and i figured the best type of loop to use to count the amount of files in a folder would be do, as it checks the condition at the end


void __fastcall TForm1::Button1Click(TObject *Sender)
{
int a = 0;
AnsiString filepath;
filepath = "\\\\server\\FMRS\\QC\\12-13\\*.*";
TSearchRec sr;
int iAttributes = 0;

if (FindFirst(filepath,iAttributes,sr)== 0);
{
do
{
a++;
Edit1->Text = a;
}
while (FindNext(sr)== 0);
}
}


now i understand whats going on above apart from

== 0


what exactly is this for? i know that == 0 means to set it as 0 , but what is it setting to 0? just playing around i removed the ==0 to see what happens.. not suprisingly it copiled, but did not give the correct results.

also, by adding

iAttributes |= faAnyFile ;

to the above will search for any file type , may they be hidden etc etc

but iattributes, i declared as a variable above .. so why am i putting |= faNyFile; afterwards .... variables only take numbers, so is faAnyFile giving it a value?

wildfrog
June 12th, 2006, 06:24 AM
now i understand whats going on above apart from

== 0


what exactly is this for? i know that == 0 means to set it as 0 , but what is it setting to 0? just playing around i removed the ==0 to see what happens.. not suprisingly it copiled, but did not give the correct results.
No, == 0 does not 'set it as 0', but it compares it to 0. So, the loop says "do while the result of FindNext equals 0".

- petter

Brad.G
June 12th, 2006, 06:29 AM
ive most probably got the wrong end of the stick here..

so it compares it to zero.. it compares findfirst to zero ? , and findnext to zero ?

why would the value of findfirst or findnext change?

Brad.G
June 12th, 2006, 06:31 AM
nvm

Return Value

findfirst returns 0 on successfully finding a file matching the search pathname.

When no more files can be found, or if there is an error in the file name:

-1 is returned

Brad.G
June 12th, 2006, 06:34 AM
ahhhhh hang on!

so if findnext becomes -1 when nothing can be found, could this be used to start a search on the next directory?

[code]
if (Findnext==-1)
{
filepath = "\\\\server\\FMRS\\QC\\12-13\\*.*";
}

but would this start the loop again?

philkr
June 12th, 2006, 06:36 AM
why would the value of findfirst or findnext change?
Because the parameter changes.

Brad.G
June 12th, 2006, 06:45 AM
so if findnext becomes -1 when nothing can be found, could this be used to start a search on the next directory?

[code]
if (Findnext==-1)
{
filepath = "\\\\server\\FMRS\\QC\\12-13\\*.*";
}

but would this start the loop again?

guess not, i just tried it

eero_p
June 12th, 2006, 07:18 AM
if (FindFirst(filepath,iAttributes,sr)== 0);
{
do
{
a++;
Edit1->Text = a;
}
while (FindNext(sr)== 0);
}



Please note that no matter what if-sentence returns, program goes into the do-while -loop. Why? Because there is a semioclon ( ; ) at the end of if -line.

Brad.G
June 12th, 2006, 07:29 AM
ahh... thanks for that eero, completely over looked that

eero_p
June 12th, 2006, 08:09 AM
ahh... thanks for that eero, completely over looked that

No problemo. :) Small things are (too) often easily missed.