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;
}
相关推荐
刚入门的大一新生3 分钟前
Linux-进程池
linux·运维·服务器
302wanger20 分钟前
DeepSeek-V4.1-Flash-1017 免费用到1024,速来
算法
lisw0538 分钟前
微分与积分的本质区别解析
算法·数学建模·极限编程
鱼很腾apoc1 小时前
【MySQL】第4期 用户管理与 C 语言连接编程详解
linux·服务器·c语言·mysql·connector/c
xys_6781 小时前
深挖数据结构:树和二叉树(上)
数据结构·算法
刚入门的大一新生1 小时前
Linux-进程间通信1
linux·运维·服务器
Interview Aid1122 小时前
Google SWE Intern VO 面经:两轮 Coding,Follow-up 考察 BFS 状态
算法
张宇Joaquin2 小时前
Linux系统下磁盘扇区512n、512e和4kn的区别
linux·运维·数据库
道虽迩,不行不至2 小时前
数据库自动连库执行增量脚本-Linux
linux·数据库
GeW2 小时前
新质生产力发展路线图来了,Linux和数据库人该怎么接
linux·数据库