先定義一個全域性的檔案 FILE *fp;讀檔案if((fp=fopen("student.txt","r"))==NULL)/*開啟檔案*/ { printf("檔案打不開!\n"); exit(1); } while(!feof(fp)) { p=(Student *)malloc(sizeof(Student)); fscanf(fp,"%s%s%s%d%d%d%d%d%d", p->num,p->name,p->clas,&p->xf.jch,&p->xf.zhy,&p->xf.xx,&p->xf.rw,&p->xf.shy,&p->xf.zx); p->next=sx->next; p->prior=sx; if(sx->next!=NULL) p->next->prior=p; sx->next=p; } fclose(fp);寫檔案if((fp=fopen("student.txt","w"))==NULL) { printf("檔案打不開!\n"); exit(1); } while(p!=NULL) { fprintf(fp,"\n%s %s %s %d %d %d %d %d %d", p->num,p->name,p->clas,p->xf.jch,p->xf.zhy,p->xf.xx,p->xf.rw,p->xf.shy,p->xf.zx); p=p->next; } fclose(fp);
先定義一個全域性的檔案 FILE *fp;讀檔案if((fp=fopen("student.txt","r"))==NULL)/*開啟檔案*/ { printf("檔案打不開!\n"); exit(1); } while(!feof(fp)) { p=(Student *)malloc(sizeof(Student)); fscanf(fp,"%s%s%s%d%d%d%d%d%d", p->num,p->name,p->clas,&p->xf.jch,&p->xf.zhy,&p->xf.xx,&p->xf.rw,&p->xf.shy,&p->xf.zx); p->next=sx->next; p->prior=sx; if(sx->next!=NULL) p->next->prior=p; sx->next=p; } fclose(fp);寫檔案if((fp=fopen("student.txt","w"))==NULL) { printf("檔案打不開!\n"); exit(1); } while(p!=NULL) { fprintf(fp,"\n%s %s %s %d %d %d %d %d %d", p->num,p->name,p->clas,p->xf.jch,p->xf.zhy,p->xf.xx,p->xf.rw,p->xf.shy,p->xf.zx); p=p->next; } fclose(fp);