CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: MFC : CFileFind

  1. #1
    Guest

    MFC : CFileFind

    Hi, I am using this class 'CFileFind' to read in all files of a certain extension. Below is the code :

    CFileFind oFinder;
    CString oFilename;

    /* Search for files */
    if (oFinder.FindFile ("*.SAV") != FALSE)
    {
    /* Loop until no file is found */
    while (oFinder.FindNextFile () == TRUE)
    {
    /* Get filename */
    oFilename = oFinder.GetFileName ();

    // Here i will use the filename to load the data
    .
    .
    }

    /* End the earch */
    oFinder.Close ();
    }

    The problem with this is that i will always read in 1 file less, e.g 20 files found, only 19 read in. I suspected I need to get the file name immediately after the 'FileFind' function is successful but in the Microsoft Help documentation, the 'GetFileName' function can be only called when 'GetNextFile' function is called at least once.

    Hope that you guys can help me. Thanks!!


  2. #2
    Join Date
    May 1999
    Location
    United Kingdom
    Posts
    136

    Re: MFC : CFileFind

    The problem with CFileFind.GetNextFile() is that it returns FALSE with the last file it finds. Thus, even though there is another file, your code exits. Do the following instead:


    BOOL bFilesLeft = TRUE;

    /* Loop until no file is found */
    do
    {
    bFilesLeft = oFinder.FindNextFile ();

    /* Get filename */
    oFilename = oFinder.GetFileName ();

    // Here i will use the filename to load the data
    .
    .
    } while (bFilesLeft);





    Hope this helps?
    Oliver.


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