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;
}
相关推荐
硅基手札11 分钟前
【Linux内核专栏 11】驱动框架:platform / char / misc / 设备树
linux·运维·服务器
Doubbbbbbble云21 分钟前
基于流处理框架的实时算法实现策略4
算法
倒头就睡的小比特26 分钟前
Linux基础指令初步认识
linux
2601_9628857228 分钟前
如何用 Python 计算 ROC 变动率指标做动量分析?
开发语言·python·算法
Logic10131 分钟前
C语言/数据结构动态规划题解:爬楼梯(含坏台阶)——避开障碍物的路径计数
c语言·数据结构·动态规划·时间复杂度·空间复杂度·算法题·爬楼梯
Starry-sky(jing)40 分钟前
watchdog: BUG: soft lockup - CPU#3 stuck for 22s! 怎么排查——从寄存器 dump 到自旋锁争用根因
linux·内核·服务器运维·soft lockup·性能排查
胡家伟++41 分钟前
我用 48 轮 AI 思维链做了一次研究战略推演:从“验证信号危机“到“验证栈“统一框架
人工智能·算法
山下梅子酒2251 小时前
洛谷-入门-B2081
c语言
DongQiShanRen1 小时前
裁决台账双向互校(上):名册与实物的第一道对账
java·linux·运维·数据库·人工智能·自然语言处理·数据挖掘
ShineWinsu1 小时前
对于Redis:AOF持久化的解析
linux·数据库·redis·缓存·面试·持久化·aof