链表到文件和文件到链表问题
程序代码:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> char ch = -1; char change = -1; typedef struct User { char ID[20]; char PW[20]; char phonum[15]; char address[100]; char name[20]; struct User *next; } unode,*Ulist; int listtofile(Ulist L,FILE *fp) { Ulist p = L; while(p) { fprintf(fp,"%s %s %s %s %s\n",L->ID,L->PW,L->phonum,L->name,L->address); p = p->next; } fclose(fp); return 1; } /* Ulist filetolist(FILE *fp) { Ulist head = (Ulist)malloc(sizeof(unode)); char ID[20]; char PW[20]; char phonum[15]; char address[100]; char name[20]; Ulist p; Ulist q; p=q=head; if(fp==NULL) { printf("打开文件失败!"); return NULL; } while(fscanf(fp,"%s %s %s %s %s",ID,PW,phonum,name,address)!=EOF) { q=(Ulist)malloc(sizeof(unode)); strcpy(q->ID,ID); strcpy(q->PW,PW); strcpy(q->phonum,phonum); strcpy(q->address,address); strcpy(q->name,name); p->next=q; p=q; } p->next=NULL; return head; } */ Ulist filetolist(FILE *fp) { Ulist head = (Ulist)malloc(sizeof(unode)); char ID[20]; char PW[20]; char phonum[15]; char address[100]; char name[20]; Ulist p; Ulist q; p=q=head; if(fp==NULL) { printf("打开文件失败!"); return NULL; } freopen("admin.txt","r",stdin); while(scanf("%s%s%s%s%s",ID,PW,phonum,name,address)!=EOF) { q=(Ulist)malloc(sizeof(unode)); strcpy(q->ID,ID); strcpy(q->PW,PW); strcpy(q->phonum,phonum); strcpy(q->address,address); strcpy(q->name,name); p->next=q; p=q; } p->next=NULL; freopen("CON","r",stdin); return head; } int checkformat(char s[])//这里可以修改特定要求格式(我只要求大于0就可) { if (strlen(s) == 0) { printf("长度应大于0!"); return 0; } else return 1; } Ulist Writein(Ulist &L,char ID[],char PW[]) { Ulist p = (Ulist)malloc(sizeof(unode)); strcpy(p->ID,ID); strcpy(p->PW,PW); p->next = L; L = p; printf("%s",L->ID); return L; } int FindUlist(Ulist L, char ID[]) { Ulist p = L; while(p) { if(strcmp(p->ID, ID) == 0) { return 1; } p = p->next; } return 0; } Ulist Search(Ulist L, char ID[], char PW[]) { Ulist p = L; while(p) { if(strcmp(p->ID, ID) == 0 && strcmp(p->PW, PW)) { return L; } p = p->next; } return NULL; } int Register(Ulist ahead, Ulist chead, Ulist &admin, Ulist &client) { while(1) { char ID[20],PW[20],CF[20];//账号,密码,确认密码 char define; printf("请输入你要注册的用户类型:\n"); printf("【1】客户\n"); printf("【2】管理员\n"); printf("【0】返回上一级菜单\n"); define = getch(); switch(define) { case '0': system("cls"); return 0;break; case '1': case '2': for (int i = 0; i < 3; i++) { printf("请输入要注册的用户名:"); scanf("%s",ID); if(!checkformat(ID))//检查格式 break; printf("\n"); printf("请输入密码:"); scanf("%s",PW); if(!checkformat(PW)) break; printf("\n"); printf("请再次确认密码:"); scanf("%s",CF); if (strcmp(PW,CF) != 0)//检查是否一致 { printf("两次密码输入不一致!\n"); Sleep(500); system("cls"); continue; } if (define == '1') { if(FindUlist(chead,ID)) printf("该用户名已存在!\n"); else { client = Writein(chead,ID,PW); printf("注册成功!\n"); Sleep(500); system("cls"); return 1; } } if (define == '2') { if(FindUlist(ahead,ID)) printf("该用户名已存在!\n"); else { client = Writein(ahead,ID,PW); printf("注册成功!\n"); Sleep(500); system("cls"); return 2; } } } break; default: printf("非法输入!请重新输入!"); Sleep(500); system("cls"); } } } int Login(Ulist ahead,Ulist chead,Ulist admin,Ulist client) { char define;//区分客户(标记1)和管理员(标记2) char ID[20],PW[20];//账号和密码 while(1) { printf("【1】客户登录\n"); printf("【2】管理员登录\n"); printf("【0】返回菜单\n"); define = getch(); Ulist permit = NULL; switch(define) { case '0': system("cls"); return 0; break; case '1': case '2'://由define决定登录类型 for (int i = 0; i < 3; i++) { printf("请输入用户名:"); scanf("%s",ID); printf("请输入密码:"); scanf("%s",PW); if (define == '1') permit = Search(chead,ID,PW); else if (define == '2') permit = Search(ahead,ID,PW); system("cls"); if(permit) break; else printf("您输入的用户名或密码错误!"); } break; default: printf("非法输入,请重新选择!"); Sleep(500); system("cls"); break; } //判断允许登录还是返回上级菜单 if (permit) { printf("欢迎回来!"); return define - '0';//由define判断登录类型 } else { printf("你输入的次数过多,请稍后重试!"); return 0; } } } void welcomenu(Ulist ahead, Ulist chead,Ulist &admin, Ulist client) { FILE *fa = fopen("admin.txt","w"); FILE *fc = fopen("client.txt","w"); while (change != 1 && change != 2) { printf("欢迎使用菜鸟驿站智能管理系统!\n"); printf("【1】用户登录\n"); printf("【2】用户注册\n"); printf("【3】退出系统\n"); ch = getch(); switch(ch) { case '1': system("cls"); change = Login(ahead,chead,admin,client); break; case '2': system("cls"); change = Register(ahead,chead,admin,client); break; case '3': system("cls"); printf("欢迎下次使用!"); listtofile(chead,fa); listtofile(ahead,fc); exit(0); default: printf("非法输入!请重新输入!"); Sleep(500); system("cls"); } } } int adminmenu(Ulist &admin) { do { printf("管理员菜单:\n"); printf("【1】到达包裹录入\n"); printf("【2】到达包裹转寄\n"); printf("【3】到达包裹统计\n"); printf("【4】待寄包裹录入\n"); printf("【5】待寄包裹删除\n"); printf("【6】待寄包裹分类\n"); printf("【0】退出登录"); ch = getch(); switch(ch) { case '1': system("cls"); break; case '2': system("cls"); break; case '3': system("cls"); break; case '4': system("cls"); break; case '5': system("cls"); break; case '6': system("cls"); break; case '0': system("cls"); return 0; default: printf("非法输入!请重新操作。"); Sleep(500); system("cls"); } system("cls"); } while(1); } int clientmenu(Ulist client) { do { printf("客户菜单:\n"); printf("【1】取快递\n"); printf("【2】寄快递\n"); printf("【3】修改资料\n"); printf("【4】反馈建议\n"); printf("【0】退出登录\n"); ch = getch(); switch(ch) { case '1': system("cls"); break; case '2': system("cls"); break; case '3': system("cls"); break; case '4': system("cls"); break; case '0': system("cls"); return 0; break; default: printf("非法输入!请重新操作。"); Sleep(500); system("cls"); } }while (1); } int main(void) { FILE *fa,*fc; fa = fopen("admin.txt","r"); fc = fopen("client.txt","r"); Ulist client = NULL;//指向登录后用户节点 Ulist chead = filetolist(fc);//头结点 Ulist admin = NULL;//指向登陆后用户结点 Ulist ahead = filetolist(fa);//头结点 fclose(fc); fclose(fa); do { ch = -1; welcomenu(ahead,chead, admin,client); switch(change) { case 1:change = clientmenu(client); break; case 2:change = adminmenu(admin);break; } } while(change == 0); return 0; } 大牛们,我查了好久资料,迫不得已发帖提问,我实在不知道为什么我编写的不能实现将用户传入文件或将文件传入用户,问题主要在listtofile和filetolist函数上,我很烦恼自己对文件和链表的掌握不熟悉,希望各位大牛能指点指点我的课设,谢谢了(有一些被我乱改了,不要介意)