嵌入式Linux标准IO,获取文件大小fgetc(),定位流获取文件大小fteel()、rewind
2019-07-13 00:09发布
生成海报
#include
#include
#include
int get_file_size(const char *file);
int main(int argc, const char *argv[])
{
if(argc < 2)
{
printf("no file name or path
");
return -1;
}
else
{
printf("total %d bytes
",get_file_size(argv[1]));
}
return 0;
}
1、fgetc()获取文件大小
int get_file_size(const char *file)
{
int count=0;
FILE *fp;
if((fp = fopen(file,"r")) == NULL)
{
perror("fopen");
return -1;
}
while(fgetc(fp) != EOF)
{
count ++;
}
if((fclose(fp)) == EOF)
{
perror("fclose");
return EOF;
}
return count;
}
运行结果
2、定位流获取文件大小fseek(),ftell()
int get_file_size(const char *file)
{
int count;
FILE *fp;
if((fp = fopen(file,"r")) == NULL)
{
perror("fopen");
return -1;
}
fseek(fp,0,SEEK_END);
count = ftell(fp);
if((fclose(fp)) == EOF)
{
perror("fclose");
return EOF;
}
return count;
}
运行结果
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮