欢迎使用CSDN-markdown编辑器

来源:互联网 发布:vga矩阵切换器 编辑:程序博客网 时间:2024/05/29 10:41
/*功能:演示了利用C语言递归遍历指定目录下的子目录和文件!说明:经修改也可以搜索文件名符合特定格式的文件,如修改“\\*.*”为“\\*.log”*/#include <iostream>   #include <string>   #include <io.h>#include "file.h"using namespace std;void filesearch(string path, int layer){    struct _finddata_t filefind;    string curr = path + "\\*.*";                           // 修改此处改变搜索条件      int done = 0, i, handle;    if ((handle = _findfirst(curr.c_str(), &filefind)) != -1)    {        //printf("%s\n", filefind.name);        while (!(done = _findnext(handle, &filefind)))        {            if (strcmp(filefind.name, "..") == 0)                continue;            for (i = 0; i <layer; i++)                printf("\t");            if ((_A_SUBDIR == filefind.attrib))              // 是目录              {                printf("[Dir]:\t%s\n", filefind.name);                curr = path + "\\" + filefind.name;                filesearch(curr, layer + 1);                  // 递归遍历子目录              }            else            {                printf("[File]:\t%s\n", filefind.name);            }        }        _findclose(handle);    }}int main(){    string path("F:\\123");//任意一处文件夹目录    filesearch(path, 0);    system("pause");    return 0;}
原创粉丝点击