C语言 fseek(f,0,SEEK_SET)函数案例详解

  #include

  #defineN5

  typedefstructstudent{

  longsno;

  charname[10];

  floatscore[3];

  }STU;

  voidfun(char*filename,STUn)

  {

  FILE*fp;

  fp=fopen(filename,"rb+");

  fseek(fp,-1L*sizeof(STU),SEEK_END);

  fwrite(&n,sizeof(STU),1,fp);

  fclose(fp);

  }

  voidmain()/*修改覆盖最后一个学生数据*/

  {

  STUt[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}};

  STUn={10006,"ZhaoSi",55,70,68},ss[N];

  inti,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("

  Theoriginaldata:

  ");

  for(j=0;j

  {

  printf("

  No:%ldName:%-8sScores:",ss[j].sno,ss[j].name);

  for(i=0;i<3;i++)printf("%6.2f",ss[j].score[i]);

  printf("

  ");

  }

  fun("student.dat",n);

  printf("

  Thedataaftermodifing:

  ");

  fp=fopen("student.dat","rb");

  fread(ss,sizeof(STU),N,fp);

  fclose(fp);

  for(j=0;j

  {

  printf("

  No:%ldName:%-8sScores:",ss[j].sno,ss[j].name);

  for(i=0;i<3;i++)printf("%6.2f",ss[j].score[i]);

  printf("

  ");

  }

  }