fseek函數(shù)是用來設(shè)定文件的當(dāng)前讀寫位置:
函數(shù)原型:int fseek(FILE *fp,long offset,int origin);
函數(shù)功能:把fp的文件讀寫位置指針移到指定的位置.
fseek(fp,20,SEEK_SET);
//意思是把fp文件讀寫位置指針從文件開始后移20個(gè)字節(jié).
ftell函數(shù)是用來獲取文件的當(dāng)前讀寫位置;
函數(shù)原型: long ftell(FILE *fp)
函數(shù)功能:得到流式文件的當(dāng)前讀寫位置,其返回值是當(dāng)前讀寫位置偏離文件頭部的字節(jié)數(shù).
ban=ftell(fp);
//是獲取fp指定的文件的當(dāng)前讀寫位置,并將其值傳給變量ban.
fseek函數(shù)與ftell函數(shù)綜合應(yīng)用:
分析:可以用fseek函數(shù)把位置指針移到文件尾,再用ftell函數(shù)獲得這時(shí)位置指針距文件頭的字節(jié)數(shù),這個(gè)字節(jié)數(shù)就是文件的長度.
原文:http://blog.csdn.net/swliao/archive/2009/09/04/4518012.aspx
- #include <stdio.h>
- main()
- {
- FILE *fp;
- char filename[80];
- long length;
- printf("Input the file name:");
- gets(filename);
- fp=fopen(filename,"rb");
- if(fp==NULL)
- printf("file not found!\n");
- else
- {
- fseek(fp,OL,SEEK_END);
- length=ftell(fp);
- printf("the file length %1d bytes\n",length);
- fclose(fp);
- }
- }