一 标准库 fseek 至尾部 读取ftell返回
c
#include <stdio.h>
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读取文件信息
c
#include <stdio.h>
#include <sys/stat.h>
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差不多
c
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
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;
}