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;
}
相关推荐
道法自然|~6 分钟前
BugkuCTF栅栏密码解题记录(原理+C语言实现)
c语言·开发语言
j_xxx404_11 分钟前
Linux:文件描述符fd
linux·运维·服务器
未既11 分钟前
逻辑卷挂载磁盘操作命令
linux·运维·服务器
yuuki23323313 分钟前
【C++】模拟实现 AVL树
java·c++·算法
dog25043 分钟前
阿基米德的有限步逼近思想求圆面积
算法
想做功的洛伦兹力11 小时前
2026/2/13日打卡
算法
李斯维1 小时前
安装 Arch Linux 到 VMware Workstation 的完全指南
linux
仟濹1 小时前
【算法打卡day7(2026-02-12 周四)算法:BFS and BFS】 3_卡码网107_寻找存在的路线_并查集
数据结构·算法·图论·宽度优先
YuTaoShao1 小时前
【LeetCode 每日一题】3713. 最长的平衡子串 I ——(解法二)暴力枚举 + 优化
算法·leetcode·职场和发展
蜡笔小马1 小时前
20.Boost.Geometry 中常用空间算法详解:crosses、densify、difference 与离散距离度量
c++·算法·boost