C语言 如何将文件里数据读入链表中,在下次用时自动导入数据

[复制链接]
查看11 | 回复3 | 2011-9-19 01:24:02 | 显示全部楼层 |阅读模式
#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);}}我已近实现了存储功能。。。。。。希望高手指点啊!!
回复

使用道具 举报

千问 | 2011-9-19 01:24:02 | 显示全部楼层
给你优化了一下,你试试,有问题再联系#includestdio.h#includestdlib.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;sqlist*inlist()/*数据的输入*/{sqlist*p,*q;sqlist*head;intn=0;printf(\"请输入名字性别部门学号:\\n\");p=q=(sqlist*)malloc(LEN);scanf(\"%s%s%s%d\",p-name,p-sex,p-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\",p-name,p-sex,p-dep,number);}q-next=NULL;returnhead;}voidsave(sqlist*head){sqlist*p;FILE*f;p=head;if(p!=NULL){f=fopen(\"amd.txt\",\"w\");do{/*charname[20];charsex[10];chardep[20];intnumber;fprintf(f,\"%5s%5s%5s]\\n\",name,sex,dep,number);printf(\"%5s%5s%5s]\\n\",p-name,p-sex,p-dep,p-number);数据如果超过5的长度,读的时候就不能正确取出来了,因此,应该按数据定义的长度来记录数据1是为了读取时方便,左对齐右补空格**/fprintf(f,\"%-21.21s%-11.11s%-21.21s%-11d\\n\",p-name,p-sex,p-dep,p-number);p=p-next;}while(p!=NULL);fclose(f);//fclose要和fopen相呼应}//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\",New-name,New-sex,New-dep,number);New-next=p-next;p-next=New;if(New-next==NULL){returnhead;}elseprintf(\"wait\");returnhead;}sqlist*read(){FILE*fp;sqlist*f,*head=NULL;//指针要习惯给它赋初值NULL,不要形成野指针charstr[128];if((fp=fopen(\"amd.txt\",\"r\"))==NULL){printf(\"打开文件失败!!\\n\");returnhead;//此时headisNULL}if(fgets(str,sizeof(str),fp)==NULL){printf(\"文件中没有数据!\\n\");fclose(fp);returnhead;//此时headisNULL}//有数据再创建链表,不要创建空表head=(sqlist*)malloc(LEN);if(head==NULL){printf(\"分配内存失败!\\n\");fclose(fp);exit(0);}head-next=NULL;sscanf(str,\"%s%s%s%d\",head-name,head-sex,head-dep,number);while(fgets(str,sizeof(str),fp)!=NULL){f=(sqlist*)malloc(LEN);f-next=NULL;sscanf(str,\"%s%s%s%d\",f-name,f-sex,f-dep,number);insertNode(head,f);}fclose(fp);returnhead;}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(\"%-21s%-11s%-21s%-11d\\n\",f-name,f-sex,f-dep,f-number);f=f-next;}while(f!=NULL);}}intmain(){sqlist*head;head=read();if(head==NULL){head=inlist();save(head);}elseprint(head);head=add(head);save(head);print(head);return0;}
回复

使用道具 举报

千问 | 2011-9-19 01:24:02 | 显示全部楼层
在main函数的开头,使用fopen去打开文件,如果存在的话就读取数据加到链表中
回复

使用道具 举报

千问 | 2011-9-19 01:24:02 | 显示全部楼层
大家可以看上面完整的代码。麻烦了。。。最好把整个代码贴出来,或者至少贴一个能运行的代码,硬看代码很费劲,还是运行调试容易点。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行