首页 > 系统相关 >Linux系统编程17-opendir,readdir与closedir.md

Linux系统编程17-opendir,readdir与closedir.md

时间:2022-10-14 22:22:26浏览次数:35  
标签:md 17 int readdir char opendir DT include 目录

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
    作用: 打开一个目录
    参数:
        - name: 需要打开的目录流
    返回值:
        DIR* 类型, 目录流
        NULL 错误

#include <dirent.h>
struct dirent *readdir(DIR *dirp);
    作用: 读取目录中数据
    参数:
        - dirp opendir 返回的结果
    返回值:
        struct dirent, 代表读取到的文件信息
        NULL, 读取到末尾或失败

        struct dirent
        {
            // 此目录进入点的inode
            ino_t d_ino;
            // 目录文件开头至此目录进入点的位移
            off_t d_off;
            // d_name 的长度, 不包含NULL字符
            unsigned short int d_reclen;
            // d_name 所指的文件类型
            unsigned char d_type;
            // 文件名
            char d_name[256];
        };

        d_type:
            DT_BLK - 块设备
            DT_CHR - 字符设备
            DT_DIR - 目录
            DT_LNK - 软连接
            DT_FIFO - 管道
            DT_REG - 普通文件
            DT_SOCK - 套接字
            DT_UNKNOWN - 未知

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
    作用: 关闭目录
    参数:
        - dirp opendir的返回值

实例:读取某个目录下所有普通文件的个数

readFileNum.c

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

int getFileNum(const char *path);

//读取某个目录下所有普通文件的个数
//格式: ./a.out dir
int main(int argc, char const *argv[])
{
    if (argc < 2)
    {
        printf("%s path\n", argv[0]);
        return -1;
    }
    int num = getFileNum(argv[1]);
    printf("普通文件个数为: %d\n", num);

    return 0;
}

//用于获取目录下所有普通文件的个数
int getFileNum(const char *path)
{
    //打开目录
    DIR *dir = opendir(path);
    if (dir == NULL)
    {
        perror("opendir err");
        exit(0);
    }

    //目录流
    struct dirent *ptr;
    //记录普通文件个数
    int total = 0;
    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);
            total += getFileNum(newpath);
        }

        if (ptr->d_type == DT_REG)
        {
            //普通文件, 统计个数
            total++;
        }
    }
	
    //关闭目录
    closedir(dir);
    return total;
}

标签:md,17,int,readdir,char,opendir,DT,include,目录
From: https://www.cnblogs.com/anqwjoe/p/16793206.html

相关文章

  • Linux系统编程18-dup和dup2.md
    dup#include<unistd.h>intdup(intoldfd);作用:复制一个新的文件描述符,指向同一个文件, 从空闲的文件描述符表中找一个最小的作为新文件描述符参数:......
  • Educational Codeforces Round 117 D
    D.X-MagicPair显然对于两个操作可以一眼识别是辗转相减可是我们怎么利用这个信息我们可以发现如果a>b;我们将更小的b替换成|a-b|那么我们显然又转回来了我们考虑......
  • 打开cmd的几种方法
    打开cmd窗口的几种方法开始+Windows系统+命令提示符Windows+R,输入cmd,回车(推荐使用)在任意的文件夹下,按住shift键+鼠标右键,打开powershell窗口资源管理器的地址栏前加......
  • DEMO: BAPI_SALESORDER_CREATEFROMDAT2 创建订单
    REPORTzdemo_va01.PARAMETERSp_kunnrTYPEkunnrDEFAULT'1004615'.PARAMETERSp_vkorgTYPEvkorgDEFAULT'S600'.PARAMETERSp_vtwegTYPEvtwegDEFAULT'10'.PARAM......
  • 操作系统导论习题解答(17. Free Space Management)
    Free-SpaceManagement使用segmentation实现虚拟内存时,我们可能会遇到上图所示情况,总共未使用的空间是20字节,但是被分成了2个10字节的内存段,如果有个15字节的程序请求CPU......
  • 17、python函数篇 内置函数、迭代器对象、异常的捕获和处理
    目录一、重要内置函数1、zip()2、filter()3、sorted()二、常见内置函数1、abs()2、all、any()3、bin、oct、hex、int()4、bytes()5、callable()6、chr、ord()7、dir()8、d......
  • Windows CMD批处理
    需要生成uuid时,可以使用WindowsSDK自带的工具uuidgen.exe如下  如果需要生成一千个,那么使用批处理生成到txt文件中,批处理脚本如下@echoonfor/l%%iin(1,1,100......
  • 前端 Blob 与 File 捎带 FormData 摘要
      Blob(BinaryLargeObject)表示二进制类型的大对象,通常是图片、视频、文档等如上图所示 一个blob对象 File 接口基于 Blob,继承了blob的功能并将其扩展使......
  • windows bat cmd 创建固定大小文件
    @echooffsetfilenum=1setfilesize=10485760settmppth="c:\tmp"setdespth="z:\test"rmdir%tmppth%/s/qmd%tmppth%md%despth%for/l%%iin(1,1,%file......
  • uni-app 171部分小细节优化
    /pages/mail/user-base/user-base.vue<template><viewclass="page"><!--导航栏--><free-nav-barshowBack:showRight="detail.friend"bgColor="bg-white">......