C++ realizes the traversal operation of each file under the folder

#include<iostream>

#include<string>

#include<io.h>

#include<cstdio>

#include<cstdlib>

#include<cstring>

using namespace std;

void HandleFile(string path,string filename)//Operate each file traversed
{     ifstream inFile(path + "/" + filename);

    string strGetLine;

    if (!inFile.is_open())
    {
        cout << filename + " open failed!" << endl;
        exit(1);
    }
    while (!inFile.eof())
    {
        getline(inFile, strGetLine);
        if (strGetLine.find("new XTOOLFUNC")!=string::npos)
        {
            //setXTOOLFUNC.insert(strGetLine.substr(strGetLine.find("new XTOOLFUNC"), strGetLine.find(";")-strGetLine.find("new XTOOLFUNC")));
            ++setXTOOLFUNC[strGetLine.substr(strGetLine.find("new XTOOLFUNC"), strGetLine.find(";") - strGetLine.find("new XTOOLFUNC"))];
        }
    }
    inFile.close();
    return;
}

void fileSearch(string path)//Traverse each file under the folder
{     long hFile = 0;     struct _finddata_t fileInfo; //_finddata_t The structure of storing various information of the file, <io.h>     string pathName;     /* means conform All files in; if not found, the folder is empty, exit; assign means clear pathName and set to path; append means add a string at the end;     c_str returns a temporary pointer of const char*;     _findfirst: search and specify the file The first instance whose name matches, if successful, it returns the handle of the first instance, otherwise it returns -1L;     Function prototype: long _findfirst( char *filespec, struct _finddata_t *fileinfo );     */     if ((hFile = _findfirst(pathName .assign(path).append("/*").c_str(), &fileInfo)) == -1)         return;









    do 
    {    
        if (strcmp(fileInfo.name, "..") && strcmp(fileInfo.name, ".") && (fileInfo.attrib == _A_SUBDIR|| fileInfo.attrib == _A_ARCH))//There is . And .. directories, you cannot enter the search; _A_SUBDIR and _A_ARCH indicate a certain attribute of the folder
        {             HandleFile(path, fileInfo.name);             fileSearch(path + "/" + fileInfo.name);         }     } while (_findnext(hFile , &fileInfo) == 0);     /*     _findnext searches for the next instance matching the file name provided by the _findfirst function, returns 0 if successful, otherwise returns -1;     _findclose ends the search;     */     _findclose(hFile);     return; }










int main ()

{

   string path="F:/Intranet/Li Jiangwei/Decryption/NewService";

   fileSearch(path);

   system("pause");

   return 0;

}

Guess you like

Origin blog.csdn.net/qq_38915078/article/details/105487301