用fseek和ftell获取文件的大小

时间:2014-05-02 18:05:54   收藏:0   阅读:308
bubuko.com,布布扣
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
    int n=0;
    FILE *fp;
    if((fp=fopen(argv[1],"r"))==NULL)
    {
        perror("fopen");
        exit(EXIT_FAILURE);
    }
    if(fseek(fp,0,SEEK_END)!=0)
    {
        perror("fseek");
        exit(EXIT_FAILURE);
    }
    if((n=ftell(fp))==-1)
    {
        perror("ftell");
        exit(EXIT_FAILURE);
    }
    printf("the size counted by fseek/ftell of the file is %d \n",n);
    printf("this is ls output:\n");
    execl("/bin/ls","ls","-l",argv[1],(char *)0);
    fclose(fp);
}
bubuko.com,布布扣

在Ubuntu中运行结果

bubuko.com,布布扣

用fseek和ftell获取文件的大小,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!