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;
}
相关推荐
贝锐7 小时前
从国产化信创设备到商用安卓终端,向日葵如何帮助企业实现统一运维管理?
linux·运维·远程控制
顶点多余7 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
Julien20048 小时前
调查和解决 SELinux 问题
linux·运维·服务器·网络·学习方法
罗西的思考9 小时前
【Agentic RL / 强化学习框架】Molt 设计解读
人工智能·算法·机器学习
hahaha601610 小时前
HLS高层次综合设计技巧--C++类和模板
图像处理·人工智能·算法·计算机视觉
小张同学a.10 小时前
Docker 容器实战 1—— docker 基础与镜像构建
linux·运维·docker·容器
多弗朗皮卡丘10 小时前
算法详解4:买卖股票的最佳时机系列(上)
算法
Brilliantwxx12 小时前
【C语言】 初入嵌入式C语言复习(基础+进阶面试题)
c语言·开发语言
Best-Wishes12 小时前
BurpSuite Pro教育版在linux系统中配置
linux·运维·服务器·网络安全·burpsuite
wuminyu13 小时前
深入剖析 Panama Off-heap 的性能损耗与开销
java·linux·c语言·jvm·c++