原文地址:fseek()作者:xiaoxin

意思是把文件指针指向文件的开头

fseek
  函数名: fseek
  功 能: 重定位流上的文件指针
  用 法: int fseek(FILE *stream,
long offset, int fromwhere);
  描 述:
函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset个字节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。

返回值: 成功,返回0,否则返回其他值。
  fseek position the file position pointer for the
file referenced by stream to the byte location calculated by
offset.

  程序例:
  #include
<stdio.h>
  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 bytesn",
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_SET: 文件开头
  SEEK_CUR: 当前位置
  SEEK_END: 文件结尾
  其中SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2.
  简言之:
  fseek(fp,100L,0);把fp指针移动到离文件开头100字节处;
  fseek(fp,100L,1);把fp指针移动到离文件当前位置100字节处;
  fseek(fp,100L,2);把fp指针退回到离文件结尾100字节处。

  使用实例:
  #include
<stdio.h>
  #define N 5
  typedef struct student {
  long sno;
  char name[10];
  float score[3];
  } STU;
  void fun(char *filename, STU n)
  {
  FILE *fp;
  fp = fopen(filename, "rb+");
  fseek(fp, -1L*sizeof(STU),SEEK_END);
  fwrite(&n, sizeof(STU), 1,
fp);
  fclose(fp);
  }
  void main()
  {
  STU t[N]={ {10001,"MaChao", 91, 92, 77},
{10002,"CaoKai", 75, 60, 88},
  {10003,"LiSi", 85, 70, 78}, {10004,"FangFang",
90, 82, 87},
  {10005,"ZhangSan", 95, 80, 88}};
  STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
  int i,j; FILE *fp;
  fp = fopen("student.dat", "wb");
  fwrite(t, sizeof(STU), N, fp);
  fclose(fp);
  fp = fopen("student.dat", "rb");
  fread(ss, sizeof(STU), N, fp);
  fclose(fp);
  printf("nThe original data :nn");
  for (j=0; j<N; j++)
  {
  printf("nNo: %ld Name: %-8s Scores:
",ss[j].sno, ss[j].name);
  for (i=0; i<3; i++) printf("%6.2f
", ss[j].score[i]);
  printf("n");
  }
  fun("student.dat", n);
  printf("nThe data after modifing :nn");
  fp = fopen("student.dat", "rb");
  fread(ss, sizeof(STU), N, fp);
  fclose(fp);
  for (j=0; j<N; j++)
  {
  printf("nNo: %ld Name: %-8s Scores:
",ss[j].sno, ss[j].name);
  for (i=0; i<3; i++) printf("%6.2f
", ss[j].score[i]);
  printf("n");
  }
  }

fseek()的更多相关文章

  1. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

  2. C中的fseek函数使用

    函数名:fseek函数 头文件:#include<stdio.h> 功能:把与fp有关的文件位置指针放到一个指定位置. 格式:  int fseek(FILE *stream, long ...

  3. Linux C 文件输入输出函数 fopen()、getc()/fgetc()、putc()/fputc()、fclose()、fprintf()、fscanf()、fgets()、fputs()、fseek()、ftell()、fgetpos()、fsetpos() 详解

      fopen(打开文件) 定义函数 FILE * fopen(const char * path,const char * mode); 函数说明 参数path字符串包含欲打开的文件路径及文件名,参 ...

  4. php使用file函数、fseek函数读取大文件效率分析

    php读取大文件可以使用file函数和fseek函数,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下. 1. 直接 ...

  5. PHP移动文件指针ftell()、fseek()、rewind()总结

    在对文件进行读写过程中,有时需要在文件中跳转.同不同位置读取,以及将数据写入到不同的位置.例如,使用文件模拟数据库保存数据,就需要移动文件指针.指针的位置是以从文件头开始的字节数度量的,默认以不同模式 ...

  6. 函数fseek() 用法(转)

    在阅读代码时,遇到了很早之前用过的fseek(),很久没有用了,有点陌生,写出来以便下次查阅. 函数功能是把文件指针指向文件的开头,需要包含头文件stdio.h fseek   函数名: fseek ...

  7. C语言 文件操作11--文件函数再讲 fseek()和ftell()

    //文件函数再讲 //fseek(),ftell(), #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdl ...

  8. matlab文件操作及读txt文件(fopen,fseek,fread,fclose)

    文件操作是一种重要的输入输出方式,即从数据文件读取数据或将结果写入数据文件.MATLAB提供了一系列低层输入输出函数,专门用于文件操作. 1.文件的打开与关闭 1)打开文件 在读写文件之前,必须先用f ...

  9. Matlab txt内容替换函数 fgetl fseek

    Data Import and Export  :Low-Level File I/O the contents of the file:    16     5     9     4     2  ...

  10. fseek的使用

    一:概述 在官方文档里,对于fseek的描述是 Move to specified position in file,移到文件的某一个特殊位置 二:语法 status = fseek(fileID, ...

随机推荐

  1. VS2012中使用纯C实现COM的小问题

    用VS2012新建C++工程都预定义了宏__cplusplus,所以引用到的都是C++的定义.但是要用C实现的话,一般都是也就不是C++的了.比如以下代码: #undef INTERFACE #def ...

  2. windows桌面添加右键环境

    1.组合键win + R,输入regedit,回车   打开注册表编辑器 2.找到目录中[HKEY_CLASSES_ROOT\Directory\Background\shell]对其右键,新建一个项 ...

  3. Unity3D之资源问题处理

    你做的东西如果是100%完整版 你就用 流媒体资源 Streaming Assets http://game.ceeger.com/Manual/StreamingAssets.html 你如果是类微 ...

  4. 【转】ButterKnife的使用--不错

    原文网址:http://www.cnblogs.com/exmyth/p/4779763.html ButterKnife是一个Android View注入的库. 1.开始使用 1.1 配置Eclip ...

  5. ADO.NET 增删查改小总结

    转自:http://www.cnblogs.com/ashu123/archive/2010/10/10/ado_1.html 三套路-----增删改 1 using System.Data.SqlC ...

  6. C# Protect the Password inside a TextBox ZZ

    If the Text property is called, it will send an WM_GETTEXT message, so it will surely be an internal ...

  7. HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

  8. Android学习笔记1:Activity与View

    推荐一系列优秀的Android开发源码 Activity是Android应用中负责与用户交互的组件. View组件是所有UI控件.容器控件的基类,View组件就是Android应用中用户实实在在看到的 ...

  9. linux 多线程基础3

    一.线程属性 线程具有属性,用pthread_attr_t表示,在对该结构进行处理之前必须进行初始化,在使用后需要对其去除初始化.我们用pthread_attr_init函数对其初始化,用pthrea ...

  10. Centos6.4_X64飞信安装