C++ method steps to traverse all files under a folder
- 2020-07-21 09:29:28
- OfStack
This article mainly introduces C++ to traverse a folder under all the files method steps, share with you, mainly to keep a note for yourself.
#include<iostream>
#include<string>
#include<io.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
void fileSearch(string path)
{
long hFile = 0;
/*
_finddata_t A structure that stores information about a file. <io.h>;
*/
struct _finddata_t fileInfo;
string pathName;
/*
\\* Represents all files that match ;
If the folder is empty, exit ;
assign Said the pathName Empty out the concurrence path;
append Means to add a string to the end ;
c_str return 1 a const char* Temporary pointer to ;
_findfirst
Searches for a number that matches the specified file name 1 On success, return" 1 Handle to an instance, otherwise returned -1L;
Function prototype: long _findfirst( char *filespec, struct _finddata_t *fileinfo );
*/
if ( ( hFile = _findfirst(pathName.assign(path).append("\\*").c_str(), &fileInfo) ) == -1)
return ;
do {
cout << path+"\\"+fileInfo.name << endl;
/*
Under the folder . and .. Directory, cannot enter search ;
_A_SUBDIR Represents folder properties ;
*/
if( strcmp(fileInfo.name,"..") && strcmp(fileInfo.name,".") && fileInfo.attrib==_A_SUBDIR )
fileSearch(path+"\\"+fileInfo.name);
} while ( _findnext(hFile, &fileInfo) == 0 );
/*
_findnext Search and _findfirst The function provides the file name to match under 1 Is returned on success 0 Otherwise return - 1 ;
_findclose Find the end ;
*/
_findclose(hFile);
return ;
}
int main()
{
string path="E:\\Git";
fileSearch(path);
system("pause");
return 0;
}