C语言 fstat函数

系列文章目录


前言

一、stat系统调用

stat系统调用系列包括了fstat、stat和lstat,它们都是用来返回“相关文件状态信息”的,三者的不同之处在于设定源文件的方式不同。

二、fstat

1.功能

由文件描述符取得文件的状态。

2.相关函数

stat、lstat、chmod、chown、readlink、utime。

3.头文件

#include <sys/stat.h>
#include <unistd.h>

4.函数声明

int fstat (int filedes, struct *buf);

5.描述

fstat() 用来将参数filedes 所指向的文件状态复制到参数buf 所指向的结构中(struct stat), fstat() 与stat() 作用完全相同,不同之处在于传入的参数为已打开的文件描述符。

6.返回值

执行成功返回0,失败返回-1,错误代码保存在errno中。

7.例子

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

main()
{
   struct stat buf;
   int fd;
   fd = open ("/etc/passwd", O_RDONLY);
   fstat (fd, &buf);
   printf("/etc/passwd file size = %d\n",(int)(buf.st_size));
}

执行结果:
/etc/passwd file size = 1656

三、struct stat结构体

struct stat{
    __dev_t st_dev;		/* Device.  */
    __field64(__ino_t, __ino64_t, st_ino);  /* File serial number. */
    __mode_t st_mode;		/* File mode.  */
    __nlink_t st_nlink;		/* Link count.  */
    __uid_t st_uid;		/* User ID of the file's owner.	*/
    __gid_t st_gid;		/* Group ID of the file's group.*/
    __dev_t st_rdev;		/* Device number, if device.  */
    __dev_t __pad1;
    __field64(__off_t, __off64_t, st_size);  /* Size of file, in bytes. */
    __blksize_t st_blksize;	/* Optimal block size for I/O.  */
    int __pad2;
    __field64(__blkcnt_t, __blkcnt64_t, st_blocks);  /* 512-byte blocks */
#ifdef __USE_XOPEN2K8
    /* Nanosecond resolution timestamps are stored in a format
       equivalent to 'struct timespec'.  This is the type used
       whenever possible but the Unix namespace rules do not allow the
       identifier 'timespec' to appear in the <sys/stat.h> header.
       Therefore we have to handle the use of this header in strictly
       standard-compliant sources special.  */
    struct timespec st_atim;		/* Time of last access.  */
    struct timespec st_mtim;		/* Time of last modification.  */
    struct timespec st_ctim;		/* Time of last status change.  */
# define st_atime st_atim.tv_sec	/* Backward compatibility.  */
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
#else
    __time_t st_atime;			/* Time of last access.  */
    unsigned long int st_atimensec;	/* Nscecs of last access.  */
    __time_t st_mtime;			/* Time of last modification.  */
    unsigned long int st_mtimensec;	/* Nsecs of last modification.  */
    __time_t st_ctime;			/* Time of last status change.  */
    unsigned long int st_ctimensec;	/* Nsecs of last status change.  */
#endif
    int __glibc_reserved[2];
};
在 Linux 系统下,我们可以使用 C 语言中的 fstat() 函数获取文件的属性信息。fstat() 函数需要一个文件描述符作为参数,并且会将文件的属性信息存储在一个结构体中,该结构体定义在头文件 `<sys/stat.h>` 中。 下面是 fstat() 函数的基本语法: ``` int fstat(int fd, struct stat *buf); ``` 其中,fd 是文件描述符,buf 是一个指向 struct stat 结构体的指针,用于存储文件的属性信息。fstat() 函数会返回 0 表示成功,-1 表示失败。 struct stat 结构体中包含了很多文件属性信息,其中一些常用的属性包括: - st_mode:文件类型和访问权限; - st_size:文件大小; - st_uid:文件所有者 ID; - st_gid:文件所有组 ID; - st_atime:文件最后访问时间; - st_mtime:文件最后修改时间; - st_ctime:文件状态改变时间。 我们可以通过访问 struct stat 结构体的成员来获取相应的属性信息。下面是一个示例代码: ```c #include <stdio.h> #include <sys/stat.h> #include <unistd.h> int main() { int fd; struct stat st; fd = open("file.txt", O_RDONLY); if (fd < 0) { perror("open"); return -1; } if (fstat(fd, &st) < 0) { perror("fstat"); return -1; } printf("File size: %ld bytes\n", st.st_size); printf("Last modification time: %s", ctime(&st.st_mtime)); close(fd); return 0; } ``` 上面的代码首先使用 open() 函数打开一个名为 file.txt 的文件,然后使用 fstat() 函数获取文件的属性信息,并将结果存储在 st 结构体中。最后,程序输出文件的大小和最后修改时间。 在使用 fstat() 函数时,需要注意以下几点: - 文件必须已经打开,且文件描述符有效; - 如果文件描述符指向一个文件,那么必须以读模式打开文件; - 如果文件描述符指向一个目录,那么 st_size 属性将返回 4096,表示目录的大小; - 如果文件描述符指向一个管道、套接字或字符设备,那么 st_size 属性将返回 0。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烫手的热山药

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值