在阅读代码时,遇到了非常早之前用过的fseek(),非常久没实用了。有点陌生,写出来以便下次查阅。

函数功能是把文件指针指向文件的开头。须要包括头文件stdio.h

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 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_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 :\n\n");
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 :\n\n");
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");
}
}

最新文章

  1. artTemplate-master的应用
  2. FibonacciSequence
  3. 二分--1043 - Triangle Partitioning
  4. [改善Java代码]使用Throwable获得栈信息
  5. TensorFlow conv2d原理及实践
  6. WPF DataTrigger数据触发器
  7. Xcode 中的断言
  8. BezierDemo开源项目的学习
  9. c语言,中缀表达式转后缀表达式并计算
  10. python练习册0006
  11. Linux date 命令
  12. odoo开发笔记 -- 搜索视图继承扩展
  13. Uber使用Swift重写APP的踩坑经历及解决方案(转载)
  14. [20170629]带过滤的复制项UI操作导致订阅全部初始化问题
  15. Java Web----------response&amp;&amp;request
  16. storm从入门到放弃(三),放弃使用 StreamId 特性
  17. &hellip;gen already exists but is not a source folder. Convert to a source folder or rename it [closed]
  18. [Clr via C#读书笔记]Cp8方法
  19. 17-7-20-electron中主进程和渲染进程区别与通信
  20. 强大的Vivado IP工具——自定义IP的使用

热门文章

  1. hdu3530Subsequence rmq
  2. facade pattern
  3. iOS缓存类的设计
  4. 【v2.x OGE课程 14】 控制使用
  5. partial 的好处
  6. cookie在vs又一次run的时候丢失
  7. NYOJ 300 &amp;amp;&amp;amp; hdu 2276 Kiki &amp;amp; Little Kiki 2 (矩阵高速功率)
  8. windbg检查常用命令
  9. Directx11学习笔记【十】 画一个简单的三角形
  10. MVC Code First (代码优先)