linux 下的文件目录操作之遍历目录

  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

      通过递归调用读取目录和文件信息去遍历整个目录:

      示例代码:

     #include <unistd.h>
    #include <stdio.h>
    #include <dirent.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <stdlib.h> void printdir(char * dir, int depth)
    {
    DIR * dp = opendir(dir);
    if (NULL == dp)
    {
    fprintf(stderr, "cannot open directory: %s\n", dir);
    return;
    }
    chdir(dir);
    struct dirent * entry;
    struct stat statbuf;
    while ((entry = readdir(dp)) != NULL)
    {
    stat(entry->d_name, &statbuf);
    if (S_ISDIR(statbuf.st_mode))
    {
    if (strcmp(".", entry->d_name) == || strcmp("..", entry->d_name) == )
    continue;
    printf("%*s%s/\n", depth, "", entry->d_name);
    printdir(entry->d_name, depth + );
    }
    else
    printf("%*s%s\n", depth, "", entry->d_name);
    //printf("%*s",4,"*"); 该函数表示输出"___*",前面输出3个空格。
    //如果是printf("%*s",4,"**");则表示输出"__**",前面输出2个空格。
    }
    chdir("..");
    closedir(dp);
    } int main(int argc, char * argv[])
    {
    char * topdir, pwd[] = ".";
    if (argc < )
    topdir = pwd;
    else
    topdir = argv[];
    printf("Directory scan of %s\n", topdir);
    printdir(topdir, );
    printf("done.\n");
    exit();
    }

      运行结果:

      

  • 阿里云国际版折扣https://www.yundadi.com

  • 阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
    标签: k8s

    “linux 下的文件目录操作之遍历目录” 的相关文章

    shutdown函数

    #include<sys/socket.h> int shutdown(int sockfd,int howto); 它比close少了2个限制: 1.close把描述符的引用计数减1,仅在该计数变为0时才关闭套接字.使用shutdown可以不管...

    遍历,enumerate

    nameList = ['ab','cd','ef'] for i in range(len(nameList)): print nameList[i]str2="bsadgfsdaf" for i,t in enumerate(str2): print i,t 0 b 1 s 2 a 3 d...

    面板数据缺失填补-stata实现线性插值法过程

    目录 一、原始数据说明 二、代码及过程解释 1.设定面板数据 2.内插法填补数据 3.外插法填补数据 三、注意事项 线性插值是指插值函数为一次多项式的插值方式其在插值节点上的插值误差为零。线性插值相比其他插值方式如抛物线插值具有简单、方便的特点。线性插值的几何意义即为概述图中利用过A...

    浙大Python题目集 PTA解析

    目录 引言 python习题链接 第一章 第二章 第三章 第四章 第五章 第六章 第七章 第六章函数 引言 业余学习python有两年多时间中间进步最快的阶段是我兼职给学生讲python课怎么讲好一个知识点、如何给学生布置合适的作业、评判学生作业和作业讲解都让我对自己所学的...

    Visual Studio高效调试手段与技巧总结(经验分享)

    目录 1、对0xCCCCCCCC、0xCDCDCDCD和0xFEEEFEEE等常见异常值的辨识度 2、在Debug下遇到报错弹框,点击重试,查看函数调用堆栈...

    【C++ / Java】char数组和string的相互转换及自动转换_java怎么把char数组转换成string

    一般的转换:#include<cstdio> #include<cstring> #include<string> using namespace std; char str[100]; string s; int main() { //scanf("%s"...