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;
}
相关推荐
Keven_1111 分钟前
算法札记:SPFA判负环算法的证明
算法
什巳13 分钟前
JAVA练习278- 和为 K 的子数组
java·学习·算法·leetcode
Jerry41 分钟前
LeetCode 347. 前 K 个高频元素
算法
Young Doro1 小时前
SAC 算法
线性代数·算法·机器学习
罗超驿1 小时前
2.算法效率的核心密码:时间复杂度和空间复杂度详解
java·数据结构·算法
:-)2 小时前
算法-堆排序
数据结构·算法·排序算法
星马梦缘2 小时前
算法设计 期末复习笔记
算法·动态规划·二分图·最小生成树·线性规划·最大流
悦儿遥遥雨13 小时前
PXE + Kickstart 无人值守批量部署系统
linux·javascript·nginx