Linux:目录遍历函数

目录遍历函数

在这里插入图片描述
在这里插入图片描述

遍历目录下的常规文件

/*

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
参数;
    -name:
        需要打开的目录的名称
返回值:
    DIR * 类型, 可理解为目录流
    错误返回NULL

description:
    the opendir() function opens a directory stream corresponding to the directory name, and returns
    a pointer to the directory stream.  The stream is positioned at the first entry in the directory.
=====================================================================================================

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

-参数:
    dirp是opendir返回的结果
-返回值:
    struct dirent,代表读取到的文件的信息
    读取结束或失败返回NULL


DESCRIPTION:
       The  readdir()  function  returns  a  pointer  to a dirent structure representing the next 
       directory entry in the directory stream pointed to by dirp.  It returns NULL on reaching
       the end of the directory stream or if an error occurred.
=======================================================================================================
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);

DESCRIPTION
       The  closedir() function closes the directory stream associated with dirp.  A successful call 
       to closedir() also closes the underlying file descriptor associated with dirp.  The directory 
       stream descriptor dirp is not available after this call.
========================================================================================================


*/

//读取当前工作目录下的文件

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

#include <stdlib.h>

//记录普通文件个数
int total = 0;

void getFileNum(const char *path);

int main(int argc, char *argv[]){
    
    

    if(argc < 2){
    
    
        printf("%s path  \n", argv[0]);
        return -1;
    }

    getFileNum(argv[1]);

    printf("regFile 's number is %d \n", total);

    return 0;
}

//获取目录下所有普通文件的个数
void getFileNum(const char *path){
    
    
    //打开目录
    DIR *dir = opendir(path);

    if(dir == NULL){
    
    
        perror("opendir");
        exit(0);
    }

    struct dirent *ptr;
    while((ptr = readdir(dir)) != NULL){
    
    
        //获取名称
        char *dname = ptr->d_name;

        //忽略./ ../
        if(strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0){
    
    
            continue;
        }

        //判断式普通文件还是目录
        if(ptr->d_type == DT_DIR){
    
    
            //目录文件 继续读取此目录
            
            //拼接文件目录
            char newpath[256];
            sprintf(newpath, "%s/%s",path, dname);
            printf("dir: %s \n", newpath);

            //递归
            getFileNum(newpath);

        }

        if(ptr->d_type == DT_REG){
    
    
            //普通文件
            char newpath[256];
            sprintf(newpath, "%s/%s",path, dname);
            printf("reg: %s \n", newpath);
            total++;
        }

    }

    closedir(dir);
}

猜你喜欢

转载自blog.csdn.net/qq_44861043/article/details/121377189
今日推荐