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;
}
相关推荐
ysa0510305 分钟前
利用数的变形简化大规模问题#数论
c++·笔记·算法
Ghost Face...8 分钟前
深入解析U-Boot命令系统
linux·运维·服务器
NiKo_W10 分钟前
Linux 网络初识
linux·网络·网络协议
ajax_beijing12 分钟前
当同一个弹性云服务器所在子网同时设置了snat和弹性公网IP时,会优先使用哪个
linux·运维·服务器
聆风吟º13 分钟前
Linux远程控制Windows桌面的cpolar实战指南
linux·运维·windows
奔跑吧邓邓子22 分钟前
【C语言实战(59)】C语言打造你的专属密码管家
c语言·开发实战·密码管家
CoookeCola29 分钟前
开源图像与视频过曝检测工具:HSV色彩空间分析与时序平滑处理技术详解
人工智能·深度学习·算法·目标检测·计算机视觉·开源·音视频
RealPluto33 分钟前
SSH连接本地VMware内的虚拟机
linux·全栈
DARLING Zero two♡39 分钟前
【优选算法】D&C-Mergesort-Harmonies:分治-归并的算法之谐
java·数据结构·c++·算法·leetcode
gfdgd xi40 分钟前
deepin 终端,但是版本是 deepin 15 的
linux·python·架构·ssh·bash·shell·deepin