zoukankan      html  css  js  c++  java
  • 学习了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);
            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;
    }

  • 相关阅读:
    电容的串联和并联的性质
    start.sh
    shell 得到当前目录路径
    Java程序远程无法执行nohup命令
    mysql windows 安装5.7
    电阻并联的性质
    电阻串联的性质
    webjars
    邮箱设置
    万用表使用注意事项
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3315392.html
Copyright © 2011-2022 走看看