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;
}
相关推荐
2401_8920709815 小时前
【Linux C++ 日志系统实战】LogFile 日志文件管理核心:滚动策略、线程安全与方法全解析
linux·c++·日志系统·日志滚动
lwx91485215 小时前
Linux-Shell算术运算
linux·运维·服务器
小O的算法实验室15 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
somi715 小时前
ARM-驱动-02-Linux 内核开发环境搭建与编译
linux·运维·arm开发
qq_3395548216 小时前
英飞凌ModusToolbox环境搭建
c语言·eclipse
双份浓缩馥芮白16 小时前
【Docker】Linux 迁移 docker 目录(软链接)
linux·docker
黄昏晓x16 小时前
Linux ---- UDP和TCP
linux·tcp/ip·udp
路溪非溪16 小时前
Linux驱动开发中的常用接口总结(一)
linux·运维·驱动开发
此刻觐神16 小时前
IMX6ULL开发板学习-01(Linux文件目录和目录相关命令)
linux·服务器·学习
张張40816 小时前
(域格)环境搭建和编译
c语言·开发语言·python·ai