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;
}
相关推荐
雅俗共赏1004 分钟前
医学图像重建中常用的迭代求解器分类
图像处理·算法
不熬夜的熬润之10 分钟前
KCF算法解析
人工智能·算法·计算机视觉·机器人
Exquisite.11 分钟前
k8s的Pod管理
linux·运维·服务器
CoderCodingNo11 分钟前
【CSP】CSP-J 2025真题 | 多边形 luogu-P14360 (相当于GESP六级水平)
开发语言·c++·算法
IMPYLH12 分钟前
Linux 的 env 命令
linux·运维·服务器·数据库
抠脚学代码15 分钟前
Linux开发--> UBoot学习
linux·学习·uboot
Magic--19 分钟前
【LeetCode 27. 移除元素】C++ 范围 for 极简实现与原理解析
c++·算法·leetcode
2301_8227828222 分钟前
C语言利用EasyX实现图形化界面的小游戏
c语言·单片机·图形化界面·lcd菜单·接口实现
A923A22 分钟前
【洛谷刷题 | 第九天】
算法·二分·洛谷
奇妙之二进制23 分钟前
后端常见分层模型
linux·服务器