# 递归遍历文件

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/15125.html | 发布：2022-12-30 | 更新：2022-12-30 | 分类：建站教程、投稿分享

// 函数功能：递归遍历文件  
#pragma warning(disable:4996)  
#include <stdio.h>  
#include <string.h>  
#include <io.h>  
int nFileNum;  
// 函数功能：浏览文件  
void scanfile(char* pPath, char* pName)  
{  
 char strFileName[256];  
 nFileNum++;  
 strcpy(strFileName, pPath);  
 strcat(strFileName, pName);  
 printf("%s/n", strFileName);  
}  
// 函数功能：浏览目录，递归  
void scandir(char *strCurPath)  
{  
 char strTemp[2048];  
 struct _finddata_t ffblk;  
 int nReturn = 0;  
 strcpy(strTemp, strCurPath);  
 strcat(strTemp, "*.*");  
 long lHandle = long(_findfirst(strTemp, &ffblk));  
 while (lHandle != -1 && nReturn != -1)  
 {  
 if(strcmp(ffblk.name, ".") == 0 || strcmp(ffblk.name, "..") == 0)  
 {  
 nReturn = _findnext(lHandle, &ffblk);  
 continue;  
 }  
 if (ffblk.attrib & _A_SUBDIR)  
 {  
 // Is Dir  
 strcpy(strTemp, strCurPath);  
 strcat(strTemp, ffblk.name);  
 strcat(strTemp, "//");  
 scandir(strTemp);  
 }  
 else  
 {  
 // Is File  
 scanfile(strCurPath, ffblk.name);  
 }  
 nReturn = _findnext(lHandle, &ffblk);  
 }  
 _findclose(lHandle);  
}

 

void main()  
{  
 nFileNum = 0;  
 scandir("c://");  
printf("/nTotal files: %d/n", nFileNum);  
}

 

![递归遍历文件](https://www.mfisp.com/wp-content/uploads/2022/01/1341ee10c736eb8_1_post.jpg)

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
