前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C/C++ 目录递归与结束递归

C/C++ 目录递归与结束递归

作者头像
微软技术分享
发布2022-12-28 17:49:44
5760
发布2022-12-28 17:49:44

今天碰到了一个问题,我打算递归遍历整个 Windows 目录,找 后缀名为 .pf 的文件,如果找到了一个符合要求的文件就返回。

下面是我最初的代码:

代码语言:javascript
复制
void findAllFile_cs(const char * path,const char * format,string &pfPath)
{
	// 路径末尾追加 '\*.*'
	char newpath[200];
    strcpy(newpath, path);
    strcat(newpath, "\\*.*");
    
   // 找到目录下的第一个文件
	_finddata_t findData;
	/*	文件信息结构体
		struct _finddata_t{
             unsigned attrib;			// 文件属性
             time_t time_create;		// 创建时的时间戳
             time_t time_access;		// 最后一次被访问时的时间戳
             time_t time_write;			// 最后一次被修改时的时间戳
             _fsize_t size;				// 文件字节大小
             char name[_MAX_FNAME];		// 文件名
        };
	*/
	long handle = _findfirst(newpath, &findData);
	if (handle == -1){return;}     
     
	// 遍历文件和文件夹
    while (_findnext(handle, &findData) == 0){
        // 文件夹
		if(findData.attrib & _A_SUBDIR){
			// 文件夹名不能有敏感字符 '.'、'..'
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0){continue;}
                
			// 进入这个文件夹继续遍历
            strcpy(newpath, path);
            strcat(newpath, "\\");
            strcat(newpath, findData.name);
			findAllFile_cs(newpath,format,pfPath);
		}
		// 文件
        else{
			// 判断是不是指定后缀的文件
            if(strstr(findData.name,format)){    
                // 输出(用来测试)
				//cout << "findData.size = " << findData.size << endl;
				//cout << "findData.name = " << findData.name << endl;
				//cout << "path = " << path << endl;
				
				// 取文件所在路径
				pfPath = path;
				return;
            }
        }
    }

	// 关闭搜索句柄
    _findclose(handle); 
}

然后是修改过后的代码:

代码语言:javascript
复制
int findAllFile_cs(const char * path,const char * format,string &pfPath)
{
	// 路径末尾追加 '\*.*'
	char newpath[200];
    strcpy(newpath, path);
    strcat(newpath, "\\*.*");
    
   // 找到目录下的第一个文件
	_finddata_t findData;
	/*	文件信息结构体
		struct _finddata_t{
             unsigned attrib;			// 文件属性
             time_t time_create;		// 创建时的时间戳
             time_t time_access;		// 最后一次被访问时的时间戳
             time_t time_write;			// 最后一次被修改时的时间戳
             _fsize_t size;				// 文件字节大小
             char name[_MAX_FNAME];		// 文件名
        };
	*/
	long handle = _findfirst(newpath, &findData);
	if (handle == -1){return 1;}     
     
	// 遍历文件和文件夹
    while (_findnext(handle, &findData) == 0){
        // 文件夹
		if(findData.attrib & _A_SUBDIR){
			// 文件夹名不能有敏感字符 '.'、'..'
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0){continue;}
                
			// 进入这个文件夹继续遍历
            strcpy(newpath, path);
            strcat(newpath, "\\");
            strcat(newpath, findData.name);
			if(findAllFile_cs(newpath,format,pfPath) == 0){break;}
		}
		// 文件
        else{
			// 判断是不是指定后缀的文件
            if(strstr(findData.name,format)){    
                // 输出(用来测试)
				//cout << "findData.size = " << findData.size << endl;
				//cout << "findData.name = " << findData.name << endl;
				//cout << "path = " << path << endl;
				
				// 取文件所在路径
				pfPath = path;
				
				return 0;
            }
        }
    }

	// 关闭搜索句柄
    _findclose(handle); 
	return 1;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-07-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档