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;
}
相关推荐
shizhenshide2 分钟前
极速响应:平均破解速度<3秒的验证码服务,为抢购爬虫而生
算法
AD钙奶-lalala2 分钟前
leetcode核心母题总结
算法·leetcode·职场和发展
CodeAllen嵌入式3 分钟前
Rust 正式成为 Linux 永久核心语言
linux·开发语言·rust
JeffDingAI7 分钟前
【CANN训练营】在CANN8.5上体验Hello World开启Ascend C学习
c语言·开发语言·人工智能·学习
水天需01016 分钟前
HISTCONTROL 介绍
linux
努力学算法的蒟蒻18 分钟前
day53(1.4)——leetcode面试经典150
算法·leetcode·面试
leiming620 分钟前
c++ transform算法
开发语言·c++·算法
裴云飞32 分钟前
Compose原理一之快照系统
算法·架构
橘颂TA32 分钟前
【剑斩OFFER】哈希表简介
数据结构·算法·散列表
小尧嵌入式33 分钟前
c++红黑树及B树B+树
开发语言·数据结构·c++·windows·b树·算法·排序算法