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;
}
相关推荐
霍霍的袁6 分钟前
【C++初阶】缺省参数(默认参数)详细讲解
开发语言·c++·算法
计算机安禾12 分钟前
【算法分析与设计】第2篇:计算模型与渐进复杂性分析
算法
生成论实验室17 分钟前
事件、信息荷与六维态势空间——每一个事件都是一次空间的弯曲
人工智能·算法·语言模型·可信计算技术·安全架构
budingxiaomoli22 分钟前
递归,搜索与回溯算法--递归
算法
风味蘑菇干23 分钟前
Stream基础题目
java·算法
沐风_ZTL25 分钟前
Ubuntu 22.04中OpenCode 安装与配置完整指南,及常问题解决办法
linux·ai·opencode
实心儿儿26 分钟前
Linux —— Linux进程信号 - 信号保存 和 信号处理
linux
KaMeidebaby34 分钟前
卡梅德生物技术快报|真核蛋白表达信号肽筛选实验全流程复盘
服务器·前端·数据库·人工智能·算法
霍霍的袁43 分钟前
【C++初阶】函数重载详细讲解
开发语言·c++·算法
心中有国也有家1 小时前
CANN 算子开发完全指南——从 TBE DSL 到算子上线全流程
人工智能·经验分享·笔记·分布式·算法