C++实现对文件夹下的每个文件进行遍历操作

#include<iostream>

#include<string>

#include<io.h>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<set>

using namespace std;

set<string> setXTOOLFUNC;  //用来保存结果

void HandleFile(string path,string filename)//对遍历的每个文件进行操作
{
    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)//遍历文件夹下的每个文件
{
    long hFile = 0;
    struct _finddata_t fileInfo;    //_finddata_t  存储文件各种信息的结构体,<io.h>
    string pathName;
    /*表示符合的所有文件;没有找到即文件夹为空,退出;assign 表示把 pathName清空并置为path;append 表示在末尾加上字符串;
    c_str 返回一个const char* 的临时指针;
    _findfirst:搜索与指定的文件名称匹配的第一个实例,若成功则返回第一个实例的句柄,否则返回-1L;
    函数原型: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))//文件夹下有 . 和 .. 目录,不能进入搜索;_A_SUBDIR 和_A_ARCH表示文件夹某种属性
        {
            HandleFile(path, fileInfo.name);
            fileSearch(path + "/" + fileInfo.name);
        }
    } while (_findnext(hFile, &fileInfo) == 0);
    /*
    _findnext 搜索与_findfirst函数提供的文件名称匹配的下一个实例,若成功则返回0,否则返回-1 ;
    _findclose 结束查找;
    */
    _findclose(hFile);
    return;
}

int main()

{

   string path="F:/内网通/李江伟/解密/NewService";

   fileSearch(path);

   system("pause");

   return 0;

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值