Q:编写一个c程序,计算指定文件的大小。
注意:
fseek(FILE *fp,long offset,int base);//重定位流上的文件指针,即将fp指向的文件的位置指针移向以base为基准,以offset为偏移量的位置。
ftell(FILE *fp);//返回当前文件指针的位置。这个位置是指当前文件指针相对于文件开头的位移量。
#include<stdio.h> int main() { FILE *myf; long f; myf=fopen("c:\test.txt","r");//打开文件test.txt fseek(myf,0,SEEK_END);//将文件指针置于最后 f=ftell(myf); //返回文件长度 fclose(myf); printf("the length of the file is %d bytes ",f); return 0; }
补充:程序中的c:\test.txt是用4.10中生成的文件。