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;
}
相关推荐
不知名XL11 小时前
day24 贪心算法 part02
算法·贪心算法
AI科技星12 小时前
时空几何:张祥前统一场论20核心公式深度总结
人工智能·线性代数·算法·机器学习·生活
令狐少侠201112 小时前
Linux 系统部署夜莺 nightingale 监控公司的watchdog
linux·运维·服务器
菜鸟233号12 小时前
力扣518 零钱兑换II java实现
java·数据结构·算法·leetcode·动态规划
信工 180212 小时前
RK3588系统烧录后扩容
linux·rk3588
Jay Chou why did12 小时前
程序启动地址0x80000000
linux
咋吃都不胖lyh12 小时前
Haversine 距离算法详解(零基础友好版)
线性代数·算法·机器学习
FPGA小c鸡12 小时前
FPGA通信基带算法完全指南:从理论到实战的DSP加速方案
算法·fpga开发
@Aurora.13 小时前
优选算法【专题三:二分查找算法】
算法
JAY_LIN——813 小时前
C-语言联合体和枚举
c语言