#include #include #include #include //定义函数 FILE *fp; //文件指针 void menu(); void add(); void search(); void modify(); void look(); void quit(); //主函数 int main() { menu(); return 0; } //通讯录结构体 struct user { char name[8]; //姓名 char place[15]; //籍贯 char tel1[15]; //电话1 char tel2[15]; //电话2 char email[20]; //电子邮箱 }user[50]; //菜单 void menu() { system("cls"); system("color f3"); int n; printf("\n\n\n\n\n"); printf("\t\t:---------通讯录---------:\n"); printf("\t\t: 1:新建 :\n"); printf("\t\t: 2:查询 :\n"); printf("\t\t: 3:修改 :\n"); printf("\t\t: 4:浏览 :\n"); printf("\t\t: 5:退出 :\n"); printf("\t\t:--输入数 <1-5>选择功能--:\n\n"); scanf("%d",&n); switch(n) { case 1: add(); break; //添加 case 2: search(); break; //查询 case 3: modify(); break; //修改 case 4: look(); break; //浏览 case 5: return; break; //退出 default: return; } } //添加用户信息 void add() { struct user one; system("color e3"); if((fp=fopen("user.txt","ab"))==NULL) { printf("打开文件失败!\n"); exit(0); } printf("\n请输入用户姓名:"); scanf("%s",one.name); printf("\n请输入用户籍贯:"); scanf("%s",one.place); printf("\n请输入用户电话号码1:"); scanf("%s",one.tel1); printf("\n请输入用户电话号码2:"); scanf("%s",one.tel2); printf("\n请输入用户电子邮箱:"); scanf("%s",one.email); fwrite(&one,sizeof(struct user),1,fp); //把数据块录入user.txt中 fclose(fp); printf("\n注册成功!\n"); system("pause"); menu(); }; //修改 void modify() { char namekey[8]; printf("输入要查询的姓名:"); scanf("%s",namekey); if((fp=fopen("user.txt","ab+"))==NULL) //打开文件 { printf("\n不能打开文件!\n"); exit(0); } int m=0,t=1; while(!feof(fp)) { fread(&user[m],sizeof(struct user),1,fp)==1; //读取结构体中一个用户信息 if(!strcmp(user[m].name,namekey)) //判断是否相同 { t=0; //若相同,则在后面if句中输出 break; } m++; } if(t) printf("未找到\n"),fclose(fp),system("pause"),menu(); else { printf("找到\n"); printf("姓名\t\t籍贯\t\t电话1\t\t电话2\t\t邮箱\n"); printf("%-8s\t",user[m].name); printf("%-15s\t",user[m].place); printf("%-15s\t",user[m].tel1); printf("%-15s\t",user[m].tel2); printf("%-20s\t",user[m].email); } printf("\n您想要修改哪条信息?\n\n1.姓名\t2.籍贯\t3.电话1\t4.电话2\t5.邮箱\t其它任意键.不修改"); //修改信息提示 int a; //修改的信息序号 scanf("%d",&a); char *newname; char nweplace[15]; char newtel1[15]; char newtel2[15]; char newemail[20]; switch(a) { case 1:scanf("%s",newname),strcpy(user[m].name,newname),printf("%s",user[m].name);break; default:break; } printf("修改成功\n"); fclose(fp); system("pause"); menu(); } //查询 void search() { char namekey[8]; printf("输入要查询的姓名:"); scanf("%s",namekey); if((fp=fopen("user.txt","r"))==NULL) //打开文件 { printf("\n不能打开文件!\n"); exit(0); } int m=0,t=0; while(!feof(fp)) { fread(&user[m],sizeof(struct user),1,fp)==1; //读取结构体中一个用户信息 if(!strcmp(user[m].name,namekey)) //判断是否相同 { t=1; //若相同,则在后面if句中输出 break; } m++; } if(t) { printf("找到\n"); printf("姓名\t\t籍贯\t\t电话1\t\t电话2\t\t邮箱\n"); printf("%-8s\t",user[m].name); printf("%-15s\t",user[m].place); printf("%-15s\t",user[m].tel1); printf("%-15s\t",user[m].tel2); printf("%-20s\t",user[m].email); } else printf("未找到\n"); fclose(fp); system("pause"); menu(); } //浏览 void look() { char c; if((fp=fopen("user.txt","r"))==NULL) //打开文件user.txt { printf("读取信息失败!\n"); exit(0); } int m=0; while(!feof(fp)) { if(fread(&user[m],sizeof(struct user),1,fp)==1) m++; } int i; printf("姓名\t\t籍贯\t\t电话1\t\t电话2\t\t邮箱\n"); for(i=0;i