Checking if a dir. entry returned by readdir is a directory, link or file. dent->d_type isn't showing the type

后端 未结 4 1468
一整个雨季
一整个雨季 2020-11-29 10:36

I am making a program which is run in a Linux shell, and accepts an argument (a directory), and displays all the files in the directory, along with their type.

Outpu

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 11:02

    I was able to use d_type on ubuntu:

        switch (readDir->d_type)
        {
        case DT_DIR:
            printf("Dir: %s\n", readDir->d_name);
            break;
        case DT_REG:
            printf("File: %s\n", readDir->d_name);
            break;
        default:
            printf("Other: %s\n", readDir->d_name);
        }
    

    The list of entry types can be found in dirent.h, (this could be different for os other than ubuntu):

    dirent.h

    #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
    

提交回复
热议问题