- 相关推荐
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语言中获取文件状态的相关函数小结10-18
C语言文件操作函数freopen详解07-13
C语言最实用的文件操作函数大全09-22
C语言文件08-28
C语言的文件概念07-18
什么是C语言函数09-26
C语言函数的定义07-13
PHP获取远程文件大小的解决方法10-27
C语言文件操作教程09-07
C语言头文件封装06-25