我是靠谱客的博主 舒适枕头,这篇文章主要介绍lseek()、ftell()、rewind()、fseek()函数,现在分享给大家,希望可以做个参考。

lseek(FILE * stream, int offset, int whence);
fseek(FILE * stream, long offset, int whence);
两函数参数whence可选四种参数:
1. SEEK_SET 参数offset 即为新的读写位置.
2. SEEK_CUR 以目前的读写位置往后增加offset 个位移量.
3. SEEK_END 将读写位置指向文件尾后再增加offset 个位移量.
4. 当whence 值为SEEK_CUR 或SEEK_END 时, 参数offet 允许负值的出现.

lseek()返回值:当调用成功时则返回目前的读写位置, 也就是距离文件开头多少个字节. 若有错误则返回-1;
fseek()返回值:当调用成功时则返回0, 若有错误则返回-1, errno 会存放错误代码.

long ftell(FILE * stream);
函数说明 ftell()用来取得文件流目前的读写位置。参数stream为已打开的文件指针

void rewind(FILE * stream);
函数说明 rewind()用来把文件流的读写位置移至文件开头。

int fstat(int fildes,struct stat *buf);
函数说明 fstat()用来将参数fildes所指的文件状态,复制到参数buf所指的结构中(struct stat)。

#include <stdio.h>
#include<stdlib.h>

int main()
{
  FILE * stream;
  long offset;
  
  char a;
  fpos_t pos;
  

	if ((stream = fopen("D:\Users\ASUS\Desktop\asd.txt", "rb"))==NULL)
	{
	    printf("%sn","can not open filen");
	    exit(0);
	} 

  fseek(stream, 5, SEEK_SET);
  printf("ftell the current offset = %dn", ftell(stream));
    
  rewind(stream);
  fgetpos(stream, &pos);
  printf("after rewind , offset = %dn", pos);
 
 //打印首字符 
   a = fgetc(stream);
   printf("%cn", a);
     
  pos = 10;
  fsetpos(stream, &pos);//设置指针位置为pos 
  printf("offset = %dn", ftell(stream));
  fclose(stream);   //关闭指针 
}

最后

以上就是舒适枕头最近收集整理的关于lseek()、ftell()、rewind()、fseek()函数的全部内容,更多相关lseek()、ftell()、rewind()、fseek()函数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部