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;
}
相关推荐
choumou_M42 分钟前
算法_1:由浅入深学习快速排序QuickSort
学习·算法·排序算法
GeW1 小时前
高端化走到深处,是数据库和Linux的事
linux
Coder_Shenshen1 小时前
罗克韦尔ACD工程归档离线变量树解析
算法·c#
程序员初一2 小时前
Linux 远程桌面有四种做法
linux·ssh
程序开发阿元3 小时前
1C1G 能跑什么:把留常驻服务和不留摆进一张内存表
linux·ssh
心仪久了会沦陷3 小时前
C语言数据结构入门——树与二叉树详解:概念、堆实现与链式遍历
c语言·数据结构·算法
小此方3 小时前
[算法实战论:动态规划]背包问题概念,01不满背包问题,01满背包问题,背包问题的滚动数组空间优化
算法·动态规划
悠仁さん4 小时前
【C++】哈希表
c++·算法·散列表
地平线开发者5 小时前
点云算法整理与常用计算模块:从数据到模型,再到工程与量化
算法·自动驾驶
xbzb5 小时前
自定义 yum 软件源:Linux 上 createrepo,Windows 上纯 Python 也能造
linux·运维·windows·python·yum·createrepo