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

执行过程:
在这里插入图片描述
这个还是很实用的,因为有时候我们只需要判断 “有没有某个东西”,而全部扫描一遍是很浪费时间的。
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值