JUST DO IT ~

我只想当个程序员

c 取文件长度.



#include <sys/stat.h>
long file_length(char *f)
{
    struct stat st;
    stat(f, &st);
    return st.st_size;
}


If you have the file stream (FILE * f):
fseek(f, 0, SEEK_END); // seek to end of file
size = ftell(f); // get current file pointer
fseek(f, 0, SEEK_SET); // seek back to beginning of file
// proceed with allocating memory and reading the file
Or,
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
fd = fileno(f); 
struct stat buf;
fstat(fd, &buf);
int size = buf.st_size;
Or, use stat, if you know the filename:
#include <sys/stat.h>
struct stat st;
stat(filename, &st);
size = st.st_size;

posted on 2014-08-08 16:00 小高 阅读(157) 评论(0)  编辑  收藏 所属分类: C

导航

<2014年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(3)

随笔分类(352)

收藏夹(19)

关注的blog

手册

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜