Linux C语言遍历文件夹

来源:互联网 发布:网络商标注册 编辑:程序博客网 时间:2024/06/05 23:42



#include <stdio.h>#include <dirent.h>#include <sys/stat.h>void List(char *path){    printf("路径为[%s]\n", path);    struct dirent* ent = NULL;    DIR *pDir;    pDir=opendir(path);    //d_reclen:16表示子目录或以.开头的隐藏文件,24表示普通文本文件,28为二进制文件,还有其他……    while (NULL != (ent=readdir(pDir)))    {        printf("reclen=%d    type=%d\t", ent->d_reclen, ent->d_type);        if (ent->d_reclen==24)        {            //d_type:4表示为目录,8表示为文件            if (ent->d_type==8)            {                printf("普通文件[%s]\n", ent->d_name);            }            if (ent->d_type==4)            {                printf("目录[%s]\n", ent->d_name);            }        }        else if(ent->d_reclen==16)        {            printf("[.]开头的子目录或隐藏文件[%s]\n",ent->d_name);        }        else        {            printf("其他文件[%s]\n", ent->d_name);        }    }}int main(int argc, char *argv[]){     List(argv[1]);     return 0;}


0 0
原创粉丝点击