c - C d_name[256] 和 NAME_MAX 定义中的 struct dirent

标签 c directory readdir dirent.h

我对在 struct dirent 定义中使用 d_name[256]NAME_MAX 感到困惑。 d_name[256] 是否表示文件名长度最多为 256 个字符?然后它还提到了 NAME_MAX(在底部引用)。所以,我的问题是 NAME_MAX 在这里如何关联以及在哪里可以找到 NAME_MAX 值和定义?

man readdir struct dirent 中定义如下。

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

它还断言

The only fields in the dirent structure that are mandated by POSIX.1 are: d_name[], of unspecified size, with at most NAME_MAX characters preceding the terminating null byte ('\0'); and (as an XSI exten‐ sion) d_ino. The other fields are unstandardized, and not present on all systems; see NOTES below for some further details.

最佳答案

NAME_MAXlimits.h 中声明。您还可以使用 pathconf()fpathconf() 来获取每个文件系统的限制。

long max = pathconf(pathname, _PC_NAME_MAX);

由于该结构已硬编码为 256,因此它实际上无法处理具有较长文件名的文件系统。所以 NAME_MAX 必须最多为 255(这确实是它在我的 OS X 机器上的值)。

关于c - C d_name[256] 和 NAME_MAX 定义中的 struct dirent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46819975/

相关文章:

c:这条线是做什么的?

C 从文件中读取,它读取 "@"

sql - 如何使用 SQL Server 检查目录是否存在?

search - 如何在Marklogic中搜索除一个目录之外的所有目录中的记录

PHP readdir( ) 返回 ". "和 ".. "条目

c - 当我从目录打印文件时,为什么会出现点 ("."和 "..")?

c - 如何读取两个文本文件并计算关键字数量?

c - 计算阶乘 > 31 时如何处理整数溢出

C:将目录中的文件列表存储到数组中

Node.js fs.readdir 递归目录搜索