文件操作学习笔记

#include <stdlib.h>
#include <stdio.h>
typedef struct student
{   
 char name[100];
 int num;
 int score;
}STUDENT;
STUDENT stu[ ]={"zhangsan",01,90,"lisi",02,95,"wangwu",03,95};
void Wf()
{
 FILE *fp;
 fp = fopen("stu.txt","wb");
 int i;
 for(i=0;i<3;i++)
 {
  fseek(fp,sizeof(STUDENT)*i,SEEK_SET);
  fwrite(&stu[i],sizeof(STUDENT),1,fp);
 }
 fclose(fp);
}
void Rf()
{
 STUDENT temp[3];

 FILE *fp;
 fp = fopen("stu.txt","rb");
 int i;
 for(i=0;i<3;i++)
 {
  fseek(fp,sizeof(STUDENT)*i,SEEK_SET);
  fread(&temp[i],sizeof(STUDENT),1,fp);
 }
 fclose(fp);
}
void main()
{  
 Wf();
 Rf();
 system("pause");
}

posted on 2012-10-24 09:28  whl-hl  阅读(122)  评论(0编辑  收藏  举报

导航