zoukankan      html  css  js  c++  java
  • 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;
    }
    

    许可协议: 文章中的代码均为学习时整理的笔记,博客中除去明确标注有参考文献的文章,其他文章【均为原创】作品,转载请务必【添加出处】,您添加出处是我创作的动力!
  • 相关阅读:
    近两年目标
    Spring使用ajax异步上传文件
    java注解
    js 点击文本框,预览选择图片
    修改服务器系统时间(包括hive)
    队列原理
    EMR目录
    2个CDH的hive数据同步
    CDH建表字符集问题
    EMR的fair-scheduler.xml
  • 原文地址:https://www.cnblogs.com/LyShark/p/15019615.html
Copyright © 2011-2022 走看看