Thread: Application Error

  1. #1
    Unregistered
    Guest

    Application Error

    Hi!


    The following function gives me an application error when I pass a filename (or wildchards) in searchFor that doesn't exist. (For example, I set path to "C:" and searchFor to "*.txt" but there's no txt-File in C:\ )

    How can I avoid that? Why does the while-loop run nevertheless there's no such file?

    Code:
    BOOL FillTreeViewWithFileNames(HWND hwndTree, LPSTR path, LPSTR searchFor, UINT level)
    {
        HANDLE hf;
        WIN32_FIND_DATA FindFileData;
        CHAR searchmask[260]="";
    
        strcpy(searchmask, path);
        strcat(searchmask, "\\");
        strcat(searchmask, searchFor);
    
        if(!FindFirstFile(searchmask, &FindFileData)) return 0;
    
        hf = FindFirstFile(searchmask, &FindFileData);
    
        if( (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) && (strcmp(FindFileData.cFileName,".")) && (strcmp(FindFileData.cFileName,"..")))
        {
          AddItemToTree(hwndTree, FindFileData.cFileName, level);
        }
    
        while(FindNextFile(hf, &FindFileData))
        {
          if( (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) && (strcmp(FindFileData.cFileName,".")) && (strcmp(FindFileData.cFileName,"..")))
          {
     	AddItemToTree(hwndTree, FindFileData.cFileName, level);
          }
        }
      
      FindClose(hf);
      
      return 1;
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    "If the function fails, the return value is INVALID_HANDLE_VALUE" not FALSE.

    False is non zero so when it returns the 0xFFFFFFFF or -1 your app considers it a success.

    Try
    Code:
    hFindData = FindFirstFile(sBuffer, &FindData);
    if ((GetLastError() != ERROR_NO_MORE_FILES) && (hFindData != INVALID_HANDLE_VALUE))
    {
    //the good stuff
       SetLastError(ERROR_SUCCESS);
    }
    //close and exit as error
    I would not call the FindFirstFile() twice, there is a better way and it is your job as a programmer to find it.

    I would also include some error check for string lenght seeing you 'add' them together. Some user may have typed in

    c:\MyDocuments\ThisUser\LastYear\LastWeek\Yesterda ysMessages\Averylongfilenamethatwillcauseabufferov errun.txt

    My boss did.


    Did you look at a listbox with the LB_DIR style for this 'explorer' style treeview?
    Last edited by novacain; 07-09-2002 at 04:04 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Unregistered
    Guest
    Thanks for your suggestions so far, I'll try what you said.

    Since there's a treeview from which the user can choose a path and no possibility for the user to input it manually, I thought a check for string length wasn't needed; but of course you're right, the user just could have such a long path/filename on his Disk! I'll add that, too.

    I have a treeview for the Path and one for the File list, but I'll also check the Listview w/ LB_DIR as u suggested, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM