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
撬动未来的支点3 小时前
DMABUF 核心概念:Linux 的“共享白板”机制
linux
ytttr8733 小时前
C语言实现Modbus TCP/IP协议客户端-服务器
服务器·c语言·tcp/ip
MicroTech20253 小时前
微算法科技MLGO推出隐私感知联合DNN模型部署和分区优化技术,开启协作边缘推理新时代
科技·算法·dnn
小冯记录编程4 小时前
深入解析C++ for循环原理
开发语言·c++·算法
我要学脑机4 小时前
C语言面试题问题+答案(claude生成)
c语言·开发语言
今麦郎xdu_4 小时前
【Linux系统】命令行参数和环境变量
linux·服务器·c语言·c++
还不秃顶的计科生4 小时前
linux下conda未安装的解决方法(离线安装linux下的conda)
linux·运维·服务器
DeeplyMind5 小时前
Linux的Dynamic debug功能
linux·dynamic debug
LJ-SEU5 小时前
win-ubuntu网络转发
linux·网络·ubuntu