什么是`S_ISREG()`,它有什么作用? [英] What is `S_ISREG()`, and what does it do?

查看:906
本文介绍了什么是`S_ISREG()`,它有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I came across the macro S_ISREG() in a C program that retrieves file attributes. Unfortunately, there isn't any basic information about this macro online. There are some more advanced discussions on it, but they go beyond what I'm looking for.

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

What is S_ISREG(), and what does it do? In the context of a program that retrieves file attributes, what purpose does it serve, and exactly what does it do?

谢谢.

推荐答案

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

S_ISREG() is a macro used to interpret the values in a stat-struct, as returned from the system call stat(). It evaluates to true if the argument(The st_mode member in struct stat) is a regular file.

请参见man statman fstatman inode(链接到inode手册页)以获取更多详细信息.这是手册页的相关部分:

See man stat, man fstat or man inode (link to inode man page) for further details. Here's the relevant part of the 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 */
       }

这篇关于什么是`S_ISREG()`,它有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆