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;
}
相关推荐
8Qi8几秒前
LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)
java·算法·leetcode·双指针·滑动窗口
minji...1 分钟前
Linux高级IO(五)epoll 的两种工作模式(LT/ET),多路转接之epoll版本的TCP服务器,对比 select/poll/epoll
linux·运维·服务器·epoll·epoll的工作模式·selectpollepoll·水平触发边缘触发
xiaoye-duck16 分钟前
《Linux系统编程》Linux 命名管道 FIFO 详解:突破亲缘限制的跨进程通信实现
linux
文青小兵16 分钟前
Linux云计算——docker镜像(三)
linux·docker·云计算
爱和冰阔落20 分钟前
【Linux系统编程】环境变量深度解析——从 fork 继承到 export 内建命令,两张表打通进程上下文
linux·c++·环境变量·系统调用
狮子座明仔23 分钟前
DeCoRL:把推理链拆成“乐团合奏“——AAAI 2026 一篇把 RLHF 推到 32B 打 GPT-4o 的工作
人工智能·深度学习·算法
QiLinkOS24 分钟前
合肥气链科技有限公司创办与未来技术应用
c语言·数据结构·c++·人工智能·单片机·嵌入式硬件·算法
妄想出头的工业炼药师30 分钟前
追踪定位大模型
算法·开源
Solis程序员32 分钟前
TreeMap 核心原理与实战
java·数据结构·算法
Dlrb121132 分钟前
数据结构-内核链表
linux·数据结构·链表·内核链表·inline·容器宏