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;
}
相关推荐
fai厅的秃头姐!2 小时前
C语言03
c语言·数据结构·算法
life_time_2 小时前
C语言(22)
c语言·开发语言
lisanndesu2 小时前
动态规划
算法·动态规划
myprogramc2 小时前
十大排序算法
数据结构·算法·排序算法
记得早睡~2 小时前
leetcode150-逆波兰表达式求值
javascript·算法·leetcode
修己xj2 小时前
算法系列之贪心算法
算法
qy发大财2 小时前
跳跃游戏(力扣55)
算法·leetcode
BingLin-Liu3 小时前
蓝桥杯备考:搜索算法之排列问题
算法·职场和发展·蓝桥杯
计算机小白一个3 小时前
蓝桥杯 Java B 组之岛屿数量、二叉树路径和(区分DFS与回溯)
java·数据结构·算法·蓝桥杯
lllsure3 小时前
Linux 实用指令
linux·物联网