【问题标题】:Members of Dirent structureDirent 结构的成员
【发布时间】:2012-10-11 01:50:11
【问题描述】:

我已经开始使用 dirent.h 库,并且在我的书中遇到了一个非常有用的“struct dirent”结构体成员,它 struct dirent *p->d_name。但不幸的是,它没有说明这个结构的任何其他成员;

我想知道这个结构的其他成员是什么以及它们用于什么?

问候

【问题讨论】:

  • 我假设您使用的是 Linux。在这种情况下,只需阅读 dirent.h 手册页 (man dirent.h)。
  • @NikosC。 No manual entry for dirent.h
  • @Hi-Angel 您缺少 POSIX 手册页包。 (无论它在你的 Linux 发行版中叫什么。在我的(Gentoo)上,它是 sys-apps/man-pages-posix)。
  • @NikosC。哇,我从来不知道这里不仅仅是默认的手册页。事实上,在我的 Kubuntu 上,它是包 manpages-posix-dev

标签: c linux dir readdir dirent.h


【解决方案1】:

结构,struct dirent指的是目录项。

http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html

在linux中定义为:

struct dirent {
    ino_t          d_ino;       /* inode number */
    off_t          d_off;       /* offset to the next dirent */
    unsigned short d_reclen;    /* length of this record */
    unsigned char  d_type;      /* type of file; not supported
                                   by all file system types */
    char           d_name[256]; /* filename */
};

参考:man readdir

或者只是在包含目录中查找“dirent.h”。

【讨论】:

  • 如另一个答案中所写,只有d_inod_namePOSIX。其余的应该避免,或者更糟糕的是非常谨慎地使用,并且只有在您了解其含义的情况下。
  • 其余字段仅用于非常简单的事情(例如确定条目是指文件还是子目录),告诉人们不要使用任何其他字段是没有意义的(因为他们几乎总是必须使用它们)。
【解决方案2】:

除了@Binyamin Sharet 的上述回答:

 off_t d_off - file offset
    unsigned short int d_reclen - length of the dirent record
    unsigned short int d_namlen - length of name
    unsigned int d_type - type of file

【讨论】:

  • 不应使用这些。它们是特定于实现的,而不是由 POSIX 定义的。您可能应该更新您的答案以反映这一点。
  • 某些文件系统(例如 ext4、xfs)在某些配置中(通常在 mkfs 时间)可以提供(部分或全部)目录条目的类型(DT_* 符号)。如果不知道,它们是 DT_UNKNOWN。 d_type 成员不可移植,但可以广泛使用。
【解决方案3】:

只有两个成员(来自wikipedia):

  • ino_t d_ino - 文件序列号
  • char d_name[] - 条目名称(不会超过 NAME_MAX 的大小)

也请查看unix spec

【讨论】:

  • 可能还有其他一些(实现或系统特定的)成员,但出于 POSIX 可移植性的原因,您不应使用它们。
猜你喜欢
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
相关资源
最近更新 更多