用法:int fseek( FILE *stram, long offset ,int position);
描述:该函数用来设置文件指针stream的位置。如果执行成功,stream 将以position 为基准,偏移offset 个字节的位置。如果执行失败,则不改变指针的位置。
position 用来设置从文件的哪个位置开始偏移,
SEEK_SET 从文件开头位置
SEEK_CUR 从文件当前位置
SEEK_END 从文件末尾位置
函数:long ftell(FILE *stream);
功能: 返回文件当前指针位置,
函数功能:使用fseek函数后在调用函数ftell就能很容易确定函数当前位置
#include<stdio.h>
long filesize(FILE *stream)
{
long curpose,length;
fseek(stream,2L,SEEK_SET);//point to second byte from file start
curpose=ftell(stream);
printf("curpose is %d\n",curpose);
fseek(stream,0L,SEEK_END); //point to file end
length=ftell(stream);
printf("curpose is %d\n",length);
fseek(stream,curpose,SEEK_SET);
return length;
}
int main()
{
FILE *stream;
stream=fopen("li.txt","w+");
fprintf(stream,"computer sample!");
printf("file size is %d \n",filesize(stream));
return 0;
}