Recursive functions are most suitable for applications where file retrieval is possible. Windows system provides FindFirstFile and FindNextFile two system calls, using these two functions, we can easily implement the file retrieval under Windows.
Source Files <demo.cpp>
#include <Windows.h> #include <stdio.h> #include <stdlib.h> #include <string> #include < iostream>using namespace Std;int num = 0;void searchfile (wstring filePath);//file Retrieval function int main () { wstring path = L "C:";//Set the root directory searchfile (path);//Search Directory printf ("Total file count:%d\n", num); system ("pause");} /** Retrieving file Functions * @param folder path */void searchfile (wstring filePath) { //Define a structure, For storing system call return function win32_find_data findData = {0}; //findfirstfile function returns a handle, The results are deposited in findData handle HANDLE =: FindFirstFile ((filepath+l "\\*.*"). C_str (), &finddata); //to determine if the handle is valid if (invalid_handle_value = = HANDLE) { & nbsp; return; } else { do &NBSP;{&NBSP;&NBsp if (finddata.cfilename[0] = = L '. ') Ignore current directory (.) And up-level directories (.. ) continue; num++;//file count plus one if (finddata.dwfileattributes&file_attribute_directory)//The file is a directory file { wstring TempFilePath = filePath + L "\ \" + finddata.cfilename; & nbsp; searchfile (TempFilePath);//recursive call, retrieving subdirectories } if (finddata.dwfileattributes&file _attribute_normal)//The file is a normal file { &nbsP wcout << filePath + L "\ \" + Finddata.cfilename < < endl; } } while ( FindNextFile (handle, &finddata));//Find the next file in the current directory } ::findclose (handle);// Close the Find handle}
Note: Wstring,wcout usage and string,cout are the same, except that all operands become wide characters.
Implementing file Retrieval under Windows