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;
}
相关推荐
kongba007几秒前
WSL与VMware冲突
linux
广州山泉婚姻13 分钟前
C 语言循环结构实现思路
c语言
黎阳之光科技管控13 分钟前
纯视觉定位赋能海关口岸 无感通关提升国门安全与效率
算法·安全
utf8mb4安全女神14 分钟前
怎么让服务器给自己的邮箱发消息【shell脚本】
linux·运维·服务器
じ☆冷颜〃23 分钟前
Picard–Lindelöf定理在CS中的应用:理论框架与算法基础
人工智能·经验分享·笔记·算法·机器学习
San813_LDD23 分钟前
[操作系统]虚拟机技术
linux·windows
不知名的老吴28 分钟前
机器学习评价之基础指标
人工智能·算法·机器学习
Felven28 分钟前
D. Divisible Pairs
算法
LuminousCPP28 分钟前
C 语言系列终章|编译与链接 + 预处理
c语言·经验分享·笔记·预处理·编译链接
源代码杀手29 分钟前
基于ROS2+Gazebo+RIVE的40项计算机视觉前沿机器人项目(含视觉算法原理与源码获取方式)
算法·计算机视觉·机器人