- 相关推荐
C语言用fstat函数获取文件的大小
一次偶然在Android的源代码中看到获取文件大小的函数,在以下范例中。用fstat这个函数可以避免这些问题。
函数原型:int fstat(int fildes, struct stat *buf);
参数说明:
fstat()用来将参数fildes所指的文件状态,复制到参数buf所指的结构中(struct stat)。
写个范例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <sys stat.h="">#include <unistd.h>//获取文件的大小 int get_file_size(int f){ struct stat st; fstat(f, &st); return st.st_size;}int main(void){ int fd = open("test.py",O_RDWR); int size ; if(fd < 0) { printf("open fair!
"); return -1 ; } size = get_file_size(fd) ; printf("size:%d字节--->%.2fK
",size,(float)size/1024); return 0 ; }</unistd.h></sys></fcntl.h></stdlib.h></stdio.h> |
【C语言用fstat函数获取文件的大小】相关文章:
有关C语言中获取文件状态的相关函数小结06-03
C语言文件操作函数05-22
C语言文件操作函数freopen详解07-13
C语言最实用的文件操作函数大全04-07
C语言超详细文件操作函数大全05-19
C语言文件操作函数总结分析(超详细)02-26
C语言文件03-02