深入探讨:linux中遍历文件夹下的所有文件
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()这三个函数主要用来遍历目录。在使用这三个函数前必