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

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

下面是我最初的代码:

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); 
}

然后是修改过后的代码:

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;
}
posted @   lyshark  阅读(357)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构

loading... | loading...
博客园 - 开发者的网上家园

点击右上角即可分享
微信分享提示