我在检索文件属性的C程序中遇到了宏S_ISREG()。不幸的是,在线没有有关此宏的任何基本信息。关于它,有一些更高级的讨论,但它们超出了我的期望。

什么是S_ISREG(),它有什么作用?在检索文件属性的程序的上下文中,该文件起什么作用,确切地起什么作用?

谢谢。

最佳答案

S_ISREG()是一个宏,用于解释从系统调用stat()返回的stat-struct中的值。如果参数(struct stat中的st_mode成员)是常规文件,则计算结果为true。

有关更多详细信息,请参见man statman fstatman 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/

10-11 23:15
查看更多