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);
if(pDir==NULL) printf("open dir faild
");
else printf("open dir ok %s
",path);
while (NULL != (ent=readdir(pDir)))
{
// printf("ent->d_reclen:%d
",ent->d_reclen);
// printf("ent->d_type:%d
",ent->d_type);
if (ent->d_reclen==24)
{
if (ent->d_type==8)
printf("%s
", ent->d_name);
else
{
printf("子目录:%s
",ent->d_name);
List(ent->d_name);
printf("返回%s
",ent->d_name);
}
}
}
}
int main(int argc, char *argv[])
{
List(argv[1]);
return 0;
}