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;
}

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

查看所有标签

猜你喜欢:

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

Functional Programming in Scala

Functional Programming in Scala

Paul Chiusano、Rúnar Bjarnason / Softbound print / 2014-9-14 / USD 44.99

Functional programming (FP) is a programming style emphasizing functions that return consistent and predictable results regardless of a program's state. As a result, functional code is easier to test ......一起来看看 《Functional Programming in Scala》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具

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

RGB CMYK 互转工具