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;
}
相关推荐
Trouvaille ~5 分钟前
【Linux】UDP Socket编程实战(四):地址转换函数深度解析
linux·服务器·网络·c++·udp·socket·地址转换函数
峥嵘life7 分钟前
Android16 【GSI】CtsMediaCodecTestCases等一些列Media测试存在Failed项
android·linux·运维·服务器·学习
达子6667 分钟前
Ubuntu的Gparted 无法扩展内存 报错umount: /sdb1: target is busy
linux·运维·ubuntu
lisanmengmeng8 分钟前
cephadm 17.2.5安装部署 (二)
linux·运维·服务器·ceph
dump linux9 分钟前
Linux 显示服务器与合成器架构详解
linux·驱动开发·3d
jaysee-sjc9 分钟前
【项目二】用GUI编程实现石头迷阵游戏
java·开发语言·算法·游戏
GS8FG10 分钟前
鲁班猫2,lubancat2,linux sdk4.19整编出现的镜像源的问题修复
linux
燃于AC之乐11 分钟前
【Linux系统编程】基础IO:从文件本质到系统操作
linux·文件系统·系统调用·文件描述符·基础io
_OP_CHEN13 分钟前
【Linux系统编程】(二十六)一文吃透 Ext 系列文件系统软硬链接:原理、实战与底层逻辑揭秘
linux·操作系统·文件系统·c/c++·硬链接·软链接·ext2文件系统
元亓亓亓13 分钟前
LeetCode热题100--169. 多数元素--简单
算法·leetcode·职场和发展