#includestdio.h#includestdlib.h#includeconio.h#defineLENsizeof(structStu)structStu*inlist();voidsave(structStu*head);structStu*add(structStu*head);voidinsertNode(structStu*head,structStu*newNode);structStu*read();voidprint(structStu*head);typedefstructStu{charname[20];charsex[10];chardep[20];intnumber;structStu*next;}sqlist;intmain(){sqlist*head;head=read();getchar();getchar();head=inlist();save(head);head=add(head);save(head);read(head);return0;}sqlist*inlist()/*数据的输入*/{sqlist*p,*q;sqlist*head;intn=0;printf(\"请输入名字性别部门学号:\\n\");p=q=(sqlist*)malloc(LEN);scanf(\"%s%s%s%d\",name,sex,dep,number);head=NULL;while(p-number!=0){n=n1;if(n==1)head=p;elseq-next=p;q=p;p=(sqlist*)malloc(LEN);scanf(\"%s%s%s%d\",name,sex,dep,number);}q-next=NULL;returnhead;}voidsave(sqlist*head){sqlist*p;FILE*f;p=head;if(p!=NULL){f=fopen(\"amd.txt\",\"w\");do{fprintf(f,\"%5s%5s%5s]\\n\",name,sex,dep,number);printf(\"%5s%5s%5s]\\n\",p-name,p-sex,p-dep,p-number);p=p-next;}while(p!=NULL);}close(f);}sqlist*add(sqlist*head){sqlist*p,*New;//sqlist*q;p=head;while(p-next!=NULL){p=p-next;}printf(\"请输入名字性别部门学号:\\n\");New=(sqlist*)malloc(LEN);scanf(\"%s%s%s%d\",name,sex,dep,number);New-next=p-next;p-next=New;if(New-next==NULL){returnhead;}elseprintf(\"wait\");returnhead;}sqlist*read(){FILE*fp;sqlist*f,*head;if((fp=fopen(\"amd.txt\",\"r\"))==NULL){printf(\"打开失败!!\");}elseprintf(\"打开成功\");head=(sqlist*)malloc(LEN);if(head==NULL){printf(\"分配内存失败!\\n\");exit(0);}head-next=NULL;f=(sqlist*)malloc(LEN);f-next=NULL;if(fread(f,LEN,1,fp)==NULL){printf(\"文件中没有数据!\\n\");}else{insertNode(head,f);while(!feof(fp)){f=(sqlist*)malloc(LEN);f-next=NULL;if(fread(f,LEN,1,fp)==1){insertNode(head,f);}elsefree(f);}}fclose(fp);returnhead;/*do{//printf(\"*\");//fscanf(fp,\"%s%s%s%d\",name,sex,dep,number);printf(\"%5s%5s%5s]\\n\",f-name,f-sex,f-dep,f-number);f=f-next;}while(fscanf(fp,\"%s%s%s%d\",name,sex,dep,number)!=EOF);close(fp);*/}voidinsertNode(sqlist*head,sqlist*newNode){sqlist*f;f=head;while(f-next!=NULL){f=f-next;}f-next=newNode;newNode-next=NULL;}voidprint(sqlist*head){sqlist*f;f=head;if(f!=NULL){do{printf(\"%5s%5s%5s]\\n\",f-name,f-sex,f-dep,f-number);f=f-next;}while(f!=NULL);}}我已近实现了存储功能。。。。。。希望高手指点啊!!
|