c linux 文件文件夹遍历

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

void listFiles(const char *path) {
    struct dirent *entry;
    DIR *dir = opendir(path);

    if (dir == NULL) {
        perror("Error opening directory");
        exit(EXIT_FAILURE);
    }

    while ((entry = readdir(dir)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }
        // Check if the entry is a directory and recursively list its contents
        if (entry->d_type == DT_DIR) {
            char subpath[256];
            printf("dir %s\n", entry->d_name);
            snprintf(subpath, sizeof(subpath), "%s/%s", path, entry->d_name);
            listFiles(subpath);
        }
        else printf("file %s\n", entry->d_name);
    
    }

    closedir(dir);
}

int main() {
    const char *folderPath = "."; // Change this to the desired folder path
    listFiles(folderPath);

    return 0;
}
相关推荐
ShineWinsu4 小时前
对于C++:继承的解析—上
开发语言·数据结构·c++·算法·面试·笔试·继承
pp起床5 小时前
动态规划 | part05
算法·动态规划
小付同学呀5 小时前
C语言学习(五)——输入/输出
c语言·开发语言·学习
GuangHeAI_ATing5 小时前
国密算法SSD怎么选?这3款国产固态硬盘安全又高速
算法
梦幻精灵_cq5 小时前
学C之路:不可或缺的main()主函数框架(Learn-C 1st)
c语言·开发语言
A.A呐5 小时前
【Linux第六章】进程状态和优先级
linux
雨泪丶5 小时前
代码随想录算法训练营-Day34
算法
Yzzz-F5 小时前
牛客寒假算法训练营2
算法
iambooo6 小时前
Shell在日志分析与故障排查中的实战应用
linux·服务器·网络
甄心爱学习6 小时前
【python】获取所有长度为 k 的二进制字符串
python·算法