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;
}
相关推荐
DanyHope13 分钟前
《LeetCode 49. 字母异位词分组:哈希表 + 排序 全解析》
算法·leetcode·哈希算法·散列表
iAkuya16 分钟前
(leetcode) 力扣100 15轮转数组(环状替代)
数据结构·算法·leetcode
杰克尼18 分钟前
蓝桥云课-5. 花灯调整【算法赛】
java·开发语言·算法
.小墨迹19 分钟前
C++学习之std::move 的用法与优缺点分析
linux·开发语言·c++·学习·算法·ubuntu
风华同学21 分钟前
【Linux驱动篇】LED驱动开发实验
linux·驱动开发·ubuntu
沪漂的码农22 分钟前
UDS诊断物理层时间参数详解技术文章
c语言·can·uds
李斯维22 分钟前
安装 WSL 最好的方式
linux·windows
努力学算法的蒟蒻26 分钟前
day38(12.19)——leetcode面试经典150
算法·leetcode·面试
搬砖魁首35 分钟前
ZK-ALU-在有限域上实现乘法和除法
算法·zk·alu·域运算·算术逻辑单元·模乘·蒙哥马利模约简
iAkuya42 分钟前
(leetcode)力扣100 17缺失的第一个正数(哈希)
算法·leetcode·哈希算法