怎么在C语言中利用fstat函数获取文件的大小

  介绍

怎么在C语言中利用fstat函数获取文件的大小?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

函数原型:<代码> int fstat (int菲尔德斯,struct stat * buf);

参数说明:

<代码> fstat() 用来将参数菲尔德斯所指的文件状态,复制到参数buf所指的结构中(struct stat)。

写个范例:

# include  & lt; stdio.h>   # include  & lt; stdlib.h>   # include  & lt; fcntl.h>   # include  & lt; sys/stat.h>   # include  & lt; unistd.h>//获取文件的大小,   int  get_file_size (int  f)   {   struct 才能stat 圣;   ,,fstat (f,和圣);   return 才能;st.st_size;   }   int 主要(空白)   {   ,int  fd =,开放(“test.py" O_RDWR);   ,int  size ;   ,如果(fd  & lt;, 0)   ,{   ,printf (“open 公平! \ n");   1,return ;   ,}=,,size  get_file_size (fd),;   ,printf(“大小:% d字节——→% .2fK \ n",大小,尺寸(浮动)/1024);   ,return  0,;   }

关于怎么在C语言中利用fstat函数获取文件的大小问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

怎么在C语言中利用fstat函数获取文件的大小