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;
}
相关推荐
tankeven3 分钟前
HJ132 小红走网格
c++·算法
小璐资源网7 分钟前
算法黑箱的可解释性危机
算法
不想看见40413 分钟前
Power of Four二进制特性--力扣101算法题解笔记
数据结构·算法
一个有温度的技术博主27 分钟前
Redis系列三:在linux上安装Redis
linux·数据库·redis
做怪小疯子27 分钟前
Leetcode刷题——8.重叠区间
算法·leetcode·职场和发展
2401_8578652330 分钟前
C++模块接口设计
开发语言·c++·算法
蓝莓星冰乐38 分钟前
第一章:C语言概述与环境搭建
c语言·开发语言
阿常呓语39 分钟前
Linux命令 date详解
linux·运维·服务器·linux command
add45a40 分钟前
嵌入式C++低功耗设计
开发语言·c++·算法
DeepModel42 分钟前
【概率分布】指数分布(Exponential Distribution)原理、推导与实战
python·算法·概率论