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 分钟前
数智孪生,全景可视——黎阳之光透明仓库,重构智慧仓储新范式
大数据·人工智能·算法·安全·数字孪生
生成论实验室11 分钟前
WOLM认知引擎:为系统赋予“知止”的生命本能——一套确定性、内生安全的通用认知决策内核
人工智能·算法·机器学习·自动驾驶·安全架构
黎阳之光16 分钟前
智慧公安视频孪生平台:构建全域治安防控可视化体系
大数据·人工智能·算法·安全·数字孪生
大鸣王潮202417 分钟前
Flow-GRPO vs Flow-Factory: SD3 GRPO 实现对比
人工智能·算法
大明者省17 分钟前
手机访问虚拟机里面的网站(从虚拟机桥接网络到宝塔面板可访问)
linux·服务器·网络
平行侠17 分钟前
40希尔排序 - 以递减间距进行插入排序
java·算法·排序算法
林熙蕾LXL19 分钟前
进程处理操作
开发语言·c++·算法
代码无bug抓狂人19 分钟前
用回溯算法解决01背包
数据结构·算法
Shan120521 分钟前
二叉树的遍历算法之中序遍历
算法
剑神一笑23 分钟前
Linux netstat 命令深度解析:从网络连接到端口监控的完整实现
linux·运维·网络