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;
}
相关推荐
年纪青青13 小时前
NanoPi Neo移植笔记(U-Boot v2025.10 + Linux Kernel 6.18 + Ubuntu 24.04 根文件系统)
linux·笔记·ubuntu·nanopi neo·linux镜像
add45a13 小时前
C++中的智能指针详解
开发语言·c++·算法
默|笙13 小时前
【Linux】进程信号(1)_信号产生
linux·服务器·网络
做科研的周师兄13 小时前
巴音河中下游灌溉草地空间分布数据集(2020年)
大数据·人工智能·算法·机器学习·数据挖掘·聚类
闻缺陷则喜何志丹13 小时前
【分治法 前缀和】P8572 [JRKSJ R6] Eltaw|普及+
c++·算法·前缀和·洛谷·分治法
纤纡.13 小时前
矿物识别分类:8 种机器学习算法对比与实战(平均值填充数据集)
python·深度学习·算法·机器学习
ulias21213 小时前
函数栈帧的创建和销毁
开发语言·数据结构·c++·windows·算法
少许极端13 小时前
算法奇妙屋(三十五)-贪心算法学习之路 2
学习·算法·贪心算法
代码探秘者13 小时前
【算法篇】3.位运算
java·数据结构·后端·python·算法·spring
Aaswk13 小时前
回溯算法的本质理解
c语言·算法·leetcode·力扣·剪枝