问题描述
#include<stdio.h>#include<string.h>structperson{charname[8];chartel[15];charqq[12];charschpro[50];};charfilename[20]="E:\我的通讯录.txt";FILE*fp;voidcreat();voidoutput();voidsearch();voidappend();voidmodify();voiddel();voidexit();voidmain(){intm;creat();while(1){printf("ntt*********欢迎使用通讯录信息管理系统*********nn");printf("ntt添加,请按1查找,请按2");printf("ntt修改,请按3删除,请按4");printf("ntt输出,请按5退出,请按0");printf("nttn");printf("ntt********************************************nn");printf("请选择(0--5):");scanf("%d",&m);if(m>=0&&m<=5){switch(m){case1:append();break;case2:search();break;case3:modify();break;case4:del();break;case5:output();break;case0:exit();}printf("nn操作完毕,请再次选择!");}elseprintf("nn选择错误,请再次选择!");}}/*输入模块creat():创建通讯录的子函数。*/voidcreat(){structpersonone;/*printf("n请输入通讯录文件名:");scanf("%s",filename);*/if((fp=fopen(filename,"at+"))==NULL){printf("n不能建立通讯录!");exit();}fprintf(fp,"%-10s%-20s%-20%-50sn","姓名","电话号码","qq号码","学校和专业");printf("n请输入姓名、电话号码、qq号码及学校专业(以0结束)n");scanf("%s",one.name);while(strcmp(one.name,"")){scanf("%s%s",one.tel,one.qq,one.schpro);fprintf(fp,"%-10s%-20s%-20sn",one.name,one.tel,one.qq,one.schpro);scanf("%s",one.name);}fclose(fp);}/*输出模块output():输出通讯录中联系人通讯信息的子函数*/voidoutput(){structpersonone;if((fp=fopen(filename,"r"))==NULL){printf("n不能打开通讯录!");exit();}printf("nn%20sn","通讯录");while(!feof(fp)){fscanf(fp,"%s%s%s%sn",one.name,one.tel,one.qq,one.schpro);printf("%-10s%-20s%-20%-50s",one.name,one.tel,one.schpro);}fclose(fp);}/*添加模块append():向通讯录中添加某人通讯信息的子函数*/voidappend(){structpersonone;if((fp=fopen(filename,"a"))==NULL){printf("n不能打开通讯录!");exit();}printf("n请输入添加的姓名、电话号码、qq号码及学校专业n");scanf("%s%s%s",one.name,one.tel,one.qq,one.schpro);fprintf(fp,"%-10s%-20s%-20%-50sn",one.name,one.tel,one.qq,one.schpro);fclose(fp);}/*查找模块search():在通讯录中查找某人通讯信息的子函数*/voidsearch(){structpersonone;intk,j=0;if((fp=fopen(filename,"rb"))==NULL){printf("n不能打开通讯录!");exit();}printf("n姓名查找请按1");printf("n电话号码查找请按2");scanf("%d",&k);switch(k){case1:{charnamekey[8];printf("n请输入姓名:");scanf("%s",namekey);while(!feof(fp)){fscanf(fp,"%s%s%s%sn",one.name,one.tel,one.qq,one.schpro);if(!strcmp(namekey,one.name)){printf("nn已查到,记录为:");printf("n%-10s%-20s%-20s0%-50s",one.name,one.tel,one.qq,one.schpro);j=1;}}if(!j)printf("nn对不起,通讯录中没有此人的记录。");fclose(fp);}break;case2:{chartelkey[15];printf("n请输入电话号码:");scanf("%s",telkey);while(!feof(fp)){fscanf(fp,"%s%s%s%sn",one.name,one.tel,one.qq,one.schpro);if(!strcmp(telkey,one.tel)){printf("nn已查到,记录为:");printf("n%-10s%-20s%-20s0%-50s",one.name,one.tel,one.qq,one.schpro);j=1;}}if(!j)printf("nn对不起,通讯录此人的记录。");fclose(fp);}break;}/*修改模块modify():在通讯录中修改某人通讯信息的子函数*/voidmodify(){structpersonone;intm,k=0;longoffset;charnamekey[8];printf("n请输入姓名:");scanf("%s",namekey);if((fp=fopen(filename,"r+"))==NULL){printf("n不能打开通讯录!");exit();}while(!feof(fp)){offset=ftell(fp);fscanf(fp,"%s%s%sn",one.name,one.tel,one.qq,one.schpro);if(!strcmp(namekey,one.name)){k=1;break;}}if(k){printf("n已查到,记录为:");printf("n%-10s%-18s%-50s",one.name,one.tel,one.qq,one.schpro);printf("n请输入新姓名、电话号码、qq号码及学校专业:");scanf("%s%s%s%s",one.name,one.tel,one.qq,one.schpro);fseek(fp,offset,SEEK_SET);printf("%ld",ftell(fp));fprintf(fp,"%-10s%-20s%-20s%-50sn",one.name,one.tel,one.qq,one.schpro);}elseprintf("n对不起,通讯录中没有此人的记录。");fclose(fp);}/*删除模块del():在通讯录中删除某人通讯信息的子函数*/voiddel(){intm,k=0,flag;longoffset1,offset2;charnamekey[8],valid[4];structpersonone;printf("n请输入姓名:");scanf("%s",namekey);if((fp=fopen(filename,"r+"))==NULL){printf("n不能打开通讯录!");exit();}while(!feof(fp)){offset1=ftell(fp);fscanf(fp,"%s%s%s%sn",one.name,one.tel,one.qq,one.schpro);if(!strcmp(namekey,one.name)){k=1;break;if(k){printf("n已查到,记录为");printf("n%-10s%-20s%-20s%-50s",one.name,one.tel,one.qq,one.schpro);printf("n确实要删除,按1;不删除,按0:");scanf("%d",&m);if(m){fseek(fp,offset1,SEEK_SET);fprintf(fp,"%-10s%-20s%-20%-50sn","","","","");}elseprintf("n对不起,通讯录中没有此人的记录。");fclose(fp);}}}}
解决方案
解决方案二:
该回复于2011-12-08 08:49:53被版主删除
解决方案三:
好像应该是你说哪里错了然后问怎么解决吧。。。这么多代码,谁会去一行行去看。。
解决方案四:
C++..
解决方案五:
引用3楼xl_0715的回复:
C++..
win-tcTurboC2.c吧
解决方案六:
了考究