LINUX下用C语言实现遍历文件夹


学习了LINUX下用C语言实现遍历文件夹,一些心得

struct dirent中的几个成员:

d_type:4表示为目录,8表示为文件

d_reclen:16表示子目录或文件,24表示非子目录

d_name:目录或文件的名称

具体代码如下,仅供参考
#include
#include
#include

void List(char *path)
{
struct dirent* ent = NULL;
DIR *pDir;
pDir=opendir(path);
while (NULL != (ent=readdir(pDir)))
{
if (ent->d_reclen==24)
{
if (ent->d_type==8)
printf("%s\n", ent->d_name);
else
{
printf("子目录:%s\n",ent->d_name);
List(ent->d_name);
printf("返回%s\n",ent->d_name);
}
}
}
}

int main(int argc, char *argv[])
{
List(argv[1]);
return 0;
}

相关内容