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;
}
相关推荐
笨笨没好名字1 分钟前
Leetcode刷题python版第一周
python·算法·leetcode
Cthy_hy6 分钟前
斯特林数:组合划分的递归经典,一二两类全解
python·算法·斯特林数
不忘不弃13 分钟前
计算pi的近似值
算法
码云骑士14 分钟前
12-GIL不是性能杀手(下)-绕过GIL的三种方案与决策树
算法·决策树·机器学习
一只齐刘海的猫17 分钟前
【Leetcode】无重复字符的最长子串
算法·leetcode·职场和发展
行智科技18 分钟前
FAST-LIVO2 源码精读(二):环境搭建与编译避坑
算法·ubuntu·自动驾驶·slam
插件开发29 分钟前
vs2015 cuda c++ cdpSimplePrint范例,递归功能实现演示
linux·c++·算法
Tisfy30 分钟前
LeetCode 2130.链表最大孪生和:转数组 / 快慢指针+链表翻转(O(1))
算法·leetcode·链表·题解
java知路30 分钟前
centos euler离线下载docker镜像
linux·docker·centos
坚果派·白晓明40 分钟前
鸿蒙 PC应用集成 hwloc:3 大 NAPI & 编译坑详解
c语言·华为·ai编程·harmonyos·atomcode