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;
}
相关推荐
浅念-2 小时前
Linux 开发环境与工具链
linux·运维·服务器·数据结构·c++·经验分享
潜创微科技--高清音视频芯片方案开发3 小时前
2026年C转DP芯片方案深度分析:从适配场景到成本性能的优选指南
c语言·开发语言
似水এ᭄往昔4 小时前
【Linux】gdb的使用
linux·运维·服务器
tian_jiangnan4 小时前
grafana白皮书
linux·服务器·grafana
Zero5 小时前
机器学习微积分--(1)核心思想
人工智能·算法·机器学习
mizuhokaga5 小时前
Linux内网集群基于Docker 安装 Chat2DB
linux·运维·docker
青桔柠薯片5 小时前
从C语言到裸机运行:i.MX6ULL 的 GPIO 控制与编译链接过程分析
c语言·开发语言·imx6ull
xiaobobo33305 小时前
c语言结构体相关箭头运算符和点号运算符的联系以及c语言的“索引”思想
c语言·箭头运算符·点号运算符·索引思想
有Li5 小时前
一种病理学内容感知变速率学习图像压缩框架 (PathoLIC)/文献速递-多模态应用技术
人工智能·深度学习·算法·计算机视觉·医学生
x_xbx5 小时前
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode