recursive file search

前端 未结 6 936
爱一瞬间的悲伤
爱一瞬间的悲伤 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:34

    There are still several bugs in your code. Try this instead:

    void find_files( wstring wrkdir )
    {
        wstring wrkdirtemp = wrkdir;
        if( !wrkdirtemp.empty() && (wrkdirtemp[wrkdirtemp.length()-1] != L'\\')  )
        {
          wrkdirtemp += L"\\";
        }
    
        WIN32_FIND_DATA file_data = {0};
        HANDLE hFile = FindFirstFile( (wrkdirtemp + L"*").c_str(), &file_data );
    
        if( hFile == INVALID_HANDLE_VALUE )
        {
             return;
        }
    
        do
        {
            if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                if( (wcscmp(file_data.cFileName, L".") != 0) && 
                    (wcscmp(file_data.cFileName, L"..") != 0) )
                {
                    find_files( wrkdirtemp + file_data.cFileName );
                }
            }
            else
            {
                if( (file_data.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) == 0 )
                {
                    results << wrkdirtemp << file_data.cFileName << endl;
                }
            }
        }
        while( FindNextFile( hFile, &file_data );
    
        FindClose( hFile );
    }
    

提交回复
热议问题