|
-
June 12th, 2006, 06:17 AM
#1
im learning loops
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
Code:
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
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
Code:
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?
-
June 12th, 2006, 06:24 AM
#2
Re: im learning loops
 Originally Posted by Brad.G
now i understand whats going on above apart from
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
-
June 12th, 2006, 06:29 AM
#3
Re: im learning loops
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?
-
June 12th, 2006, 06:31 AM
#4
Re: im learning loops
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
-
June 12th, 2006, 06:34 AM
#5
Re: im learning loops
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?
-
June 12th, 2006, 06:36 AM
#6
Re: im learning loops
 Originally Posted by Brad.G
why would the value of findfirst or findnext change?
Because the parameter changes.
Please don't forget to rate users who helped you!
-
June 12th, 2006, 06:45 AM
#7
Re: im learning loops
 Originally Posted by Brad.G
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
-
June 12th, 2006, 07:18 AM
#8
Re: im learning loops
 Originally Posted by Brad.G
Code:
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.
'You help me, and I, in turn, am helped by you!'
-H.S.
-
June 12th, 2006, 07:29 AM
#9
Re: im learning loops
ahh... thanks for that eero, completely over looked that
-
June 12th, 2006, 08:09 AM
#10
Re: im learning loops
 Originally Posted by Brad.G
ahh... thanks for that eero, completely over looked that
No problemo. Small things are (too) often easily missed.
'You help me, and I, in turn, am helped by you!'
-H.S.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|