首页 > 其他分享 >fstat函数

fstat函数

时间:2023-08-23 17:25:32浏览次数:30  
标签:__ last struct int st include fstat 函数

功能

由文件描述符获取文件的状态

头文件

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

函数声明

int fstat(int filedes, struct *buf);

两个参数分别为:

int filedes: 已经打开的文件描述符,通常由 open函数返回

struct *buf: 用来存放fstat所获取的文件的状态。

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

返回值

执行成功返回0,否则返回-1

例子

#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];
};

标签:__,last,struct,int,st,include,fstat,函数
From: https://www.cnblogs.com/hi-wind/p/17652239.html

相关文章

  • fprintf()函数
    fprintf()用于文件操作#include<stdio.h>intfprintf(FILE*stream,constchar*format,...);fprintf()函数根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件.因此fprintf()可以使得信息输出到指定的文件.比如charname[20]="Mary";FILE*out;out=f......
  • 【C语言基础语法】5. 函数
    一、函数概述1.1函数的作用函数的使用可以省去重复代码的编写,降低代码重复率函数可以让程序更加模块化,从而有利于程序的阅读,修改完善1.2函数的调用:产生随机数调用函数时,需要关心:头文件:包含指定的头文件函数名字:函数名字必须和头文件声明的名字一样功能:需要知道此函数作用后才能调......
  • 无涯教程-PHP Online Quiz函数
    以下测验提供与PHP相关的多项选择题(MCQ)。您将必须阅读所有给定的答案,然后单击正确的答案。如果您不确定答案,则可以使用显示答案按钮检查答案。您可以使用下一个测验按钮检查测验中的新问题集。Q1-关于PHP,以下哪项是正确的?A-PHP可以访问cookie变量并设置cookie。......
  • SG函数
    SG函数先定义,SG函数对应有向无环图(DAG)上的一种游戏:有一枚棋子在起点上,每次可以沿着边往后移动,谁无法移动谁就输了。公平组合游戏可以转换成他,只需要将局面中的所有状态看成一个节点,合法行动看成有向边。判断必胜需要求解的就是起点的SG。对于终点(没有出边),\(SG=0\)。对于其......
  • 无涯教程-PHP Interview Questions函数
    亲爱的读者,这些PHP编程语言面试问题是专门设计的,目的是让您熟悉在采访中可能会遇到的关于PHP编程语言主题的问题的性质。根据我的经验,优秀的面试官几乎不会计划在面试过程中提出任何特定的问题,通常,问题是从该主题的一些基本概念开始的,然后根据进一步的讨论和您的回答继续......
  • 两种修改字节码改变函数执行方式
    1示例publicReturnTypefunction(){try{Object[]args=newObject[]{};RetbeforeRet=callOnBefore(args...);//返回对象if(beforeRet.state==1){return(ReturnType)beforeRet.respo......
  • 无涯教程-PHP - 错误处理函数
    这些是处理错误处理和日志记录的功能。它们使您可以定义自己的错误处理规则,以及修改错误记录方式。运行时配置这些功能的行为受php.ini中的设置影响,这些设置在下面定义。NameDefaultChangeableChangelogerror_reportingNULLPHP_INI_ALLdisplay_errors"1"PHP_INI_ALL......
  • 无涯教程-PHP - preg_split()函数
    preg_split()-语法arraypreg_split(stringpattern,stringstring[,intlimit[,intflags]]);preg_split()函数的操作与split()完全相同,只不过正则表达式被接受为pattern的输入参数。如果指定了可选的输入参数limit,则仅返回子字符串的限制数量。标志可以是以下标志......
  • 5、oracle迁移到postgres-oracle中使用的`nvl`函数更改为统一的`coalesce`函数
    目录oracle迁移到postgres-oracle中使用的nvl函数更改为统一的coalesce函数1、oracle的nvl函数2、postgre的coalesce函数oracle迁移到postgres-oracle中使用的nvl函数更改为统一的coalesce函数nvl函数与coalesce函数都是值非空时,给默认值,oracle中也存在coalesce函数1、oracle的......
  • 4、oracle迁移到postgres-oracle中使用的`decode`函数使用`case when`统一语法
    目录oracle迁移到postgres-oracle中使用的decode函数使用casewhen统一语法1、oracle的decode语法2、postgres的casewhenoracle迁移到postgres-oracle中使用的decode函数使用casewhen统一语法oracle中也有使用casewhen语法,使用decode函数比较简洁。1、oracle的decode语法匹......