C语言计算文件大小

通过stat函数获取文件的大小,单位bytes;无需将文件读入内存,可以计算大文件;
 
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

int stat(const char *pathname, struct stat *buf);


int main(int argc,char **argv)
{
    struct stat statbuf;

    if(stat(argv[1], &statbuf))
    {
        printf("get file(%s) stat fail.\n",argv[1]);
    }
    printf("input file path is = %s\n", argv[1]);
    printf("file size is %llu bytes\n",(unsigned long long int)statbuf.st_size);
    return 0;
}

 

posted @ 2021-06-04 22:02  Grooovvve  阅读(497)  评论(0编辑  收藏  举报