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;
}
相关推荐
向日葵花籽儿9 分钟前
#运维 | 前端 # Linux http.server 实践:隐藏长文件名,简短路径 (http://IP:port/别名 ) 访问
linux·运维·前端
特立独行的猫a17 分钟前
C/C++三方库移植到HarmonyOS平台详细教程(补充版so库和头文件形式)
c语言·c++·harmonyos·napi·三方库·aki
zh_xuan1 小时前
LeeCode 40.组合总和II
c语言·数据结构·算法
都叫我大帅哥2 小时前
动态规划:从懵逼到装逼,一篇让你彻底搞懂DP的终极指南
java·算法
艾莉丝努力练剑3 小时前
《递归与迭代:从斐波那契到汉诺塔的算法精髓》
c语言·学习·算法
我一定会有钱8 小时前
Linux爆音问题解决方法(隔一会会有奇怪噪音)
linux·运维·服务器
超级皮皮8 小时前
力扣热题之stack
算法·leetcode·职场和发展
weixin_470740368 小时前
某算法的python执行汇编
汇编·python·算法
是乐谷9 小时前
燧原科技招大模型训练算法工程师
科技·算法
YuTaoShao9 小时前
【LeetCode 热题 100】139. 单词拆分——(解法一)记忆化搜索
java·算法·leetcode·职场和发展