资源描述:
《链表读取存储》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#includestructkang{chara[20];charb[20];charc[20];chard[50];structkang*next;};voidzhang(){printf("");printf("");printf("");printf("");}structkang*creat1(structkang*p1){chara;structkang*q;structkang*p;q=p1;if(p1!=NULL){while(p1->next!
2、=NULL){p1=p1->next;}}printf("是否输入?(yorn)");scanf("%c",&a);while(a=='y'
3、
4、a=='Y'){p=(structkang*)malloc(sizeof(structkang));printf("请输入:");scanf("%s",p->a);printf("请输入:");scanf("%s",p->b);printf("请输入:");scanf("%s",p->c);printf("请输入:");scanf("%s",p->d);p->next=NULL;if(p1==NULL){p1=p;q=p;
5、}else{p1->next=p;p1=p1->next;}getchar();printf("是否输入?(yorn)");scanf("%c",&a);}returnq;}voidsave(structkang*p){FILE*fp;if((fp=fopen("kang.txt","w"))==NULL){printf("文件创建失败!");return;}while(p!=NULL){fprintf(fp,"%s%s%s%s",p->a,p->b,p->c,p->d);p=p->next;}fclose(fp);}structkang*open(){structkang*
6、pHead=NULL;structkang*p=NULL;structkang*temp=NULL;FILE*pfile=NULL;pfile=fopen("kang.txt","r");inti=0;while(1){//创建一个节点temp=newstructkang;intflag=fscanf(pfile,"%s%s%s%s",temp->a,temp->b,temp->c,temp->d);i++;//文件读取结束则返回pHead**********************注意不用if(feof(pfile))if((flag==0)
7、
8、(flag==-1)){printf
9、("Readstudentfromissecced!");returnpHead;if(i==1)returnNULL;}temp->next=NULL;//假如是第一个节点if(p==NULL){p=temp;pHead=temp;printf("pHeadisfinished!");}//假如是第二个节点以上else{p->next=temp;p=p->next;p->next=NULL;}}fclose(pfile);}voidprint(structkang*p){while(p!=NULL){printf("%s,%s,%s,%s",p->a,p->b,p->c
10、,p->d);p=p->next;}}voidmain(){structkang*p,*p1;p=NULL;p=open();p=creat1(p);p1=p;print(p1);save(p);}