CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2006
    Posts
    159

    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
    Code:
    == 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
    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?

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: im learning loops

    Quote Originally Posted by Brad.G
    now i understand whats going on above apart from
    Code:
    == 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

  3. #3
    Join Date
    Jan 2006
    Posts
    159

    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?

  4. #4
    Join Date
    Jan 2006
    Posts
    159

    Red face 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

  5. #5
    Join Date
    Jan 2006
    Posts
    159

    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?

  6. #6
    Join Date
    Jul 2005
    Location
    Germany
    Posts
    1,194

    Re: im learning loops

    Quote 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!

  7. #7
    Join Date
    Jan 2006
    Posts
    159

    Re: im learning loops

    Quote 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

  8. #8
    Join Date
    Oct 2005
    Posts
    199

    Re: im learning loops

    Quote 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.

  9. #9
    Join Date
    Jan 2006
    Posts
    159

    Re: im learning loops

    ahh... thanks for that eero, completely over looked that

  10. #10
    Join Date
    Oct 2005
    Posts
    199

    Re: im learning loops

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured