Chinaunix首页 | 论坛 | 博客
  • 博客访问: 217544
  • 博文数量: 36
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 374
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-04 18:21
文章分类

全部博文(36)

文章存档

2011年(1)

2009年(5)

2008年(30)

我的朋友

分类: C/C++

2008-09-20 10:21:45

  名: fseek
  功 能: 重定位流上的文件指针
  用 法: int fseek(FILE *stream, long offset, int fromwhere);
  描 述: 函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。
  返回值: 成功,返回0,否则返回其他值。
  程序例:
  #include
  long filesize(FILE *stream);
  int main(void)
  {
  FILE *stream;
  stream = fopen("MYFILE.TXT", "w+");
  fprintf(stream, "This is a test");
  printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
  fclose(stream);
  return 0;
  }
  long filesize(FILE *stream)
  {
  long curpos, length;
  curpos = ftell(stream);
  fseek(stream, 0L, SEEK_END);
  length = ftell(stream);
  fseek(stream, curpos, SEEK_SET);
  return length;
  }
  int fseek( FILE *stream, long offset, int origin );
  第一个参数stream为文件指针
  第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移
  第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET
  SEEK_CUR: 当前位置
  SEEK_END: 文件结尾
  SEEK_SET: 文件开头
  其中SEEK_CUR,SEEK_END和SEEK_SET依次为1,2和0
阅读(2798) | 评论(0) | 转发(0) |
0

上一篇:编辑词条fwrite

下一篇:fread

给主人留下些什么吧!~~