重定位文件指针fseek

用法: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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值