recursive file search

前端 未结 6 941
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 13:49

I\'m trying to figure out how to work this thing out .. For some reason, it ends at a certain point.. I\'m not very good at recursion and I\'m sure the problem lies somewher

6条回答
  •  轮回少年
    2021-01-06 14:44

    From the FindFirstFile documentation:

    If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate.

    You should only exit from the one iteration not the whole program:

       if( fHandle == INVALID_HANDLE_VALUE )
       {
         return;
       }
    

    And this may solve your other problem:

    else if( file_data.dwFileAttributes != FILE_ATTRIBUTE_HIDDEN && 
       file_data.dwFileAttributes != FILE_ATTRIBUTE_SYSTEM  &&
       wcscmp(file_data.cFileName, L".") != 0 && 
       wcscmp(file_data.cFileName, L"..") != 0
     )
    {
        results << wrkdir << "\\" << file_data.cFileName << endl;
    }
    

    Also see @fretje's answer as well. It gives another problem that your code has.

    Updated new: You need to use fHandle as a local variable as well, not global variable.

    Change to:

     HANDLE fHandle = FindFirstFile( temp.c_str(), &file_data );
    

提交回复
热议问题