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;
}
相关推荐
ajassi20005 小时前
linux C 语言开发 (八) 进程基础
linux·运维·服务器
ChillJavaGuy5 小时前
常见限流算法详解与对比
java·算法·限流算法
sali-tec5 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
..过云雨5 小时前
05.【Linux系统编程】进程(冯诺依曼体系结构、进程概念、进程状态(注意僵尸和孤儿)、进程优先级、进程切换和调度)
linux·笔记·学习
Gu_shiwww5 小时前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
matlab的学徒5 小时前
Web与Nginx网站服务(改)
linux·运维·前端·nginx·tomcat
Insist7536 小时前
prometheus安装部署与alertmanager邮箱告警
linux·运维·grafana·prometheus
你怎么知道我是队长6 小时前
C语言---循环结构
c语言·开发语言·算法
艾醒6 小时前
大模型面试题剖析:RAG中的文本分割策略
人工智能·算法
BAGAE6 小时前
MODBUS 通信协议详细介绍
linux·嵌入式硬件·物联网·硬件架构·iot·嵌入式实时数据库·rtdbs