C读取文件大小的几种方式

一 标准库 fseek 至尾部 读取ftell返回

#include 

int main(int argc, char** argv)
{
    char* filename=argv[1];
    FILE* fp = fopen(filename, "r");
    long int size ;
    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    printf("%s size is %ld\n",filename,size);
    fclose(fp);
    return 0;
}

二 stat读取文件信息

#include 
#include 

int main(int argc, char** argv)
{
    char* filename=argv[1];
    struct stat fstat;
    stat(filename,&fstat);
    printf("%s size is %ld\n",filename,fstat.st_size);
    return 0;
}

三 lseek 和fseek差不多

#include 
#include 
#include 

int main(int argc, char** argv)
{
   char* filename=argv[1];
    int fd = open(argv[1],O_RDONLY);
    long int size = 0;
    size = lseek(fd,0,SEEK_END);
    printf("%s size is %ld\n",filename,size);
    close(fd);
    return 0;
}

你可能感兴趣的:(c语言,算法,开发语言)