C/C++遍历目录

栏目: C++ · 发布时间: 6年前

内容简介:C/C++遍历目录d_type的取值

C/C++遍历目录

目录文件的表示

struct dirent { 
             ino_t      d_ino;                /* inode的编号*/
             __uint16_t d_reclen;             /* 这条记录的长度 */
             __uint8_t  d_type;               /* 文件类型 */
             __uint8_t  d_namlen;             /* 文件名的长度,中文占两个*/
             char    d_name[255 + 1];   /* 文件名,根据系统会有不同*/
     };

d_type的取值

#define DT_UNKNOWN       0
#define DT_FIFO          1
#define DT_CHR           2
#define DT_DIR           4
#define DT_BLK           6
#define DT_REG           8
#define DT_LNK          10
#define DT_SOCK         12
#define DT_WHT          14

实例

#include <iostream>
#include <dirent.h> // 或 #include <sys/dir.h>

int main() {
    DIR *dp; // 目录指针
    struct dirent *dirp; // 目录文件指针

    dp = opendir("."); // 打开一个目录

    if (!dp)
    {
        return 0;
    }

    // 读取目录问价
    while ((dirp = readdir(dp)) != nullptr) {
        std::cout << "file name=" << dirp->d_name
                  << ", name len=" << dirp->d_namlen
                  << ", ino=" << dirp->d_ino
                  << ", reclen=" << dirp->d_reclen
                  << ", seek off=" << dirp->d_seekoff
                  << ", type=" << dirp->d_type
                  << std::endl;
    }

    closedir(dp);

    return 0;
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Web Designer's Idea Book

The Web Designer's Idea Book

Patrick Mcneil / How / 2008-10-6 / USD 25.00

The Web Designer's Idea Book includes more than 700 websites arranged thematically, so you can find inspiration for layout, color, style and more. Author Patrick McNeil has cataloged more than 5,000 s......一起来看看 《The Web Designer's Idea Book》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

随机密码生成器
随机密码生成器

多种字符组合密码