首页 > 系统 > Linux > 正文

深入探讨:linux中遍历文件夹下的所有文件

2024-08-28 00:02:09
字体:
来源:转载
供稿:网友
linux C 遍历目录及其子目录
复制代码 代码如下:
#include <stdio.h> 
#include <string.h>
#include <stdlib.h> 
#include <dirent.h> 
#include <sys/stat.h> 
#include <unistd.h> 
#include <sys/types.h>
using namespace std;
void listDir(char *path) 

        DIR              *pDir ; 
        struct dirent    *ent  ; 
        int               i=0  ; 
        char              childpath[512]; 

        pDir=opendir(path); 
        memset(childpath,0,sizeof(childpath)); 

 
        while((ent=readdir(pDir))!=NULL) 
        { 

                if(ent->d_type & DT_DIR) 
                { 

                        if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0) 
                                continue; 

                        sprintf(childpath,"%s/%s",path,ent->d_name); 
                        printf("path:%s/n",childpath); 

                        listDir(childpath); 

                } 
else
{
cout<<ent->d_name<<endl;
}
        } 



int main(int argc,char *argv[]) 

        listDir(argv[1]); 
        return 0; 
}

Linux C :遍历输出指定目录下的所有文件
在Linux下opendir()、readdir()和closedir()这三个函数主要用来遍历目录。在使用这三个函数前必
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表