我在检索文件属性的 C 程序中遇到宏 S_ISREG()
。不幸的是,网上没有关于这个宏的任何基本信息。关于它有一些更高级的讨论,但它们超出了我正在寻找的范围。
什么是 S_ISREG()
,它有什么作用?在检索文件属性的程序上下文中,它的用途是什么,它究竟做了什么?
谢谢。
最佳答案
S_ISREG() 是一个宏,用于解释从系统调用 stat() 返回的 stat-struct 中的值。如果参数(struct stat 中的 st_mode 成员)是一个常规文件,它的计算结果为真。
请参阅 man stat
、man fstat
或 man inode
( link to inode man page ) 了解更多详细信息。这是手册页的相关部分:
Because tests of the above form are common, additional macros are defined by POSIX to allow the test of the file type in st_mode to be written more concisely:
S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
The preceding code snippet could thus be rewritten as:
stat(pathname, &sb);
if (S_ISREG(sb.st_mode)) {
/* Handle regular file */
}
关于c - 什么是 `S_ISREG()` ,它有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40163270/