#include <stdio.h> struct stu{ char name[10]; int num; int age; char addr[16]; }boya[2],boyb[2]; struct stu *pa,*pb; main(){ FILE *fp; char ch; int i; pa=boya; pb=boyb; //open a file fp=fopen("stulist","wb+"); printf("input data "); for(i=0;i<2;i++,pa++){ scanf("%s%d%d%s",pa->name,&pa->num,&pa->age,pa->addr); } fwrite(boya,sizeof(struct stu),2,fp); rewind(fp); fread(pb,sizeof(struct stu),2,fp); printf("%10s%10s%10s%10s ","name","number","age","address"); for(i=0;i<2;i++){ printf("%10s%10d%10d%10s ",pb->name,pb->num,pb->age,pb->addr); pb++; } fclose(fp); }
#include <stdio.h> struct stu{ char name[10]; int num; int age; char addr[15]; }boy,*p; int main(){ FILE *fp; char ch; int i; fp=fopen("stulist","rb"); printf("name number age addr "); p=&boy; for(i=0;i<2;i++){ fseek(fp,i*sizeof(struct stu),0); fread(p,sizeof(struct stu),1,fp); //printf("%s %d %d %s ",(*p).name,(*p).num,(*p).age,(*p).addr); printf("%s %d %d %s ",p->name,p->num,p->age,p->addr); p++; } }