C/C++遍历目录

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

内容简介: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;
}

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

查看所有标签

猜你喜欢:

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

Discrete Mathematics and Its Applications

Discrete Mathematics and Its Applications

Kenneth H Rosen / McGraw-Hill Science/Engineering/Math / 2003-04-22 / USD 132.81

Discrete Mathematics and its Applications is a focused introduction to the primary themes in a discrete mathematics course, as introduced through extensive applications, expansive discussion, and deta......一起来看看 《Discrete Mathematics and Its Applications》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具