c语言编程:用文件实现通讯录管理怎么做?具体点。急用,谢谢!
发布网友
发布时间:2022-05-05 11:11
我来回答
共1个回答
热心网友
时间:2022-04-23 11:49
#include<stdio.h>
#include<string.h>
typedef struct Friends
{
char sName[30];
int tel;
int sMovtel;
int home;
char Emaile[30];
char assort[30];
}Friends;
int c;
void jianli ();
void search ();
void amend ();
void change ();
void input (Friends *p);
void output (Friends *p);
void sdele ();
void fugai ();
int main()
{
int a,b=1;
char s;
while(b==1)
{
printf("\t\t通讯录\n");
printf("\t1、新建\n");
printf("\t2、查找\n");
printf("\t3、修改\n");
printf("\t4、删除\n");
printf("请选择.......\n");
scanf("%d",&a);
switch(a)
{
case 1:jianli ();break;
case 2:search ();break;
case 3:amend ();break;
case 4:sdele();break;
}
printf("是否返回主菜单('Y'or'N')\n");
fflush(stdin);
scanf("%c",&s);
if(s=='y'|| s=='Y')
b=1;
else
break;
}
return 0;
}
/*新建*/
void jianli ()
{
int b=1;
char d;
Friends x;
FILE *fp;
fp=fopen("e:\\通讯录.txt","ab");
while (b==1)
{
input(&x);
fwrite(&x,sizeof(x),1,fp);
printf("是否继续新建('Y'or'N')?\n");
fflush(stdin);
scanf("%c",&d);
if(d=='y'||d=='Y')
{
b=1;
}
else
break;
}
fclose(fp);
}
/*查找*/
void search ()
{
int h , j = 1 , n=0 , num ;
char k[20];
FILE *fp;
Friends y;
printf("\t1、按号码查找\n");
printf("\t2、按姓名查找\n");
printf("请选择...........\n");
scanf("%d",&h);
if(h==1)
{
printf("\t请输入你查找的号码!\n");
while(j==1)
{
scanf("%d",&num);
fp=fopen("e:\\通讯录.txt","rb");
while(!feof(fp))
{
fread(&y,sizeof(y),1,fp);
if(num==y.sMovtel)
{
output(&y);
break;
}
}
if(num!=y.sMovtel)
{
printf("你输入的号码有误!!!\n请重新输入\n");
j=1;
}
fclose(fp);
}
}
if(h==2)
{
printf("\t请输入你查找的姓名!\n");
while(j==1)
{
scanf("%s",k);
fp=fopen("e:\\通讯录.txt","rb");
while(!feof(fp))
{
fread(&y,sizeof(y),1,fp);
if(strcmp(k,y.sName)==0)
{
output(&y);
break;
}
}
if(strcmp(k,y.sName)!=0)
{
printf("你输入的姓名有误!!!\n请重新输入\n");
j=1;
}
fclose(fp);
}
}
}
/*修改*/
void amend ()
{
int h , j = 1 , n=0 , num ;
char k[20];
FILE *fp;
Friends y;
printf("\t1、按号码查找你所需要修改的原始资料\n");
printf("\t2、按姓名查找你所需要修改的原始资料\n");
printf("请选择...........\n");
scanf("%d",&h);
if(h==1)
{
printf("\t请输入查找你的号码!\n");
while(j==1)
{
scanf("%d",&num);
fp=fopen("e:\\通讯录.txt","rb");
while(!feof(fp))
{
c++;
fread(&y,sizeof(y),1,fp);
if(num==y.sMovtel)
{
change ();
break;
}
}
if(num!=y.sMovtel)
{
printf("你输入的号码有误!!!\n请重新输入\n");
j=1;
}
fclose(fp);
}
}
if(h==2)
{
printf("\t请输入你查找的姓名!\n");
while(j==1)
{
c++;
scanf("%s",k);
fp=fopen("e:\\通讯录.txt","rb");
while(!feof(fp))
{
fread(&y,sizeof(y),1,fp);
if(strcmp(k,y.sName)==0)
{
change ();
break;
}
}
if(strcmp(k,y.sName)!=0)
{
printf("你输入的姓名有误!!!\n请重新输入\n");
j=1;
}
fclose(fp);
}
}
}
/*删除*/
void sdele()
{
int h , j = 1 , n=0 , num ;
char k[20];
FILE *fp;
Friends y;
printf("\t1、按号码查找你所需要修改的原始资料\n");
printf("\t2、按姓名查找你所需要修改的原始资料\n");
printf("请选择...........\n");
scanf("%d",&h);
if(h==1)
{
printf("\t请输入查找你的号码!\n");
while(j==1)
{
scanf("%d",&num);
fp=fopen("e:\\通讯录.txt","rb");
while(!feof(fp))
{
fread(&y,sizeof(y),1,fp);
if(num==y.sMovtel)
{
fugai ();
break;
}
}
if(num!=y.sMovtel)
{
printf("你输入的号码有误!!!\n请重新输入\n");
j=1;
}
fclose(fp);
}
}
if(h==2)
{
printf("\t请输入你查找的姓名!\n");
while(j==1)
{
scanf("%s",k);
fp=fopen("e:\\通讯录.txt","rb");
while(!feof(fp))
{
fread(&y,sizeof(y),1,fp);
if(strcmp(k,y.sName)==0)
{
fugai ();
break;
}
}
if(strcmp(k,y.sName)!=0)
{
printf("你输入的姓名有误!!!\n请重新输入\n");
j=1;
}
fclose(fp);
}
}
}
void change ()/*用于修改*/
{
int f,j;
FILE *fp;
Friends z ;
j=c*133;
printf("你要修改的内容:\n");
printf("\t1、姓名\n\t2、手机号\n\t3、固定电话号码\n");
printf("\t4、电子信箱\n\t5、家庭住址、\n\t6、分类\n");
printf("请选择...........\n");
scanf("%d",&f);
printf("输入你的新信息!\n");
switch(f)
{
case 1:fp=fopen("e:\\通讯录.txt","wb");
fread(&z,sizeof(z),1,fp);
fseek(fp,j,SEEK_SET);
scanf("%s",z.sName);
fwrite(&z.sName,sizeof(z.sName),1,fp);
fclose(fp);
output (&z);
break;
fclose(fp);
case 2:fp=fopen("e:\\通讯录.txt","rb");
fread(&z,sizeof(z),1,fp);
fseek(fp,(j+31),SEEK_SET);
scanf("%d",&z.sMovtel);
fwrite(&z.sMovtel,sizeof(z.sMovtel),1,fp);
fclose(fp);
break;
case 3:fp=fopen("e:\\通讯录.txt","rb");
fread(&z,sizeof(z),1,fp);
fseek(fp,(j+35),SEEK_SET);
scanf("%d",&z.tel);
fwrite(&z.tel,sizeof(z.tel),1,fp);
fclose(fp);
case 4:fp=fopen("e:\\通讯录.txt","rb");
fread(&z,sizeof(z),1,fp);
fseek(fp,(j+73),SEEK_SET);
scanf("%s",z.Emaile);
fwrite(&z.Emaile,sizeof(z.Emaile),1,fp);
fclose(fp);
break;
case 5:fp=fopen("e:\\通讯录.txt","rb");
fread(&z,sizeof(z),1,fp);
fseek(fp,(j+69),SEEK_SET);
scanf("%d",&z.home);
fwrite(&z.home,sizeof(z.home),1,fp);
fclose(fp);
break;
case 6:fp=fopen("e:\\通讯录.txt","rb");
fread(&z,sizeof(z),1,fp);
fseek(fp,(j+103),SEEK_SET);
scanf("%d",&z.assort);
fwrite(&z.assort,sizeof(z.assort),1,fp);
fclose(fp);
break;
}
}
void input (Friends *p)/*输入*/
{
printf("请输入姓名:\n");
scanf("%s",p->sName);
printf("请输入手机号码:\n");
scanf("%d",&p->sMovtel);
printf("请输入固定电话号码:\n");
scanf("%d",&p->tel);
printf("请输入电子信箱:\n");
scanf("%s",p->Emaile);
printf("输入家庭住址");
scanf("%s",p->home);
printf("请分类:\n");
scanf("%s",p->assort);
}
void output (Friends *p)/*输出*/
{
printf("**********************\n");
printf("%s\n",p->sName);
printf("%d\n",p->sMovtel);
printf("%d\n",p->tel);
printf("%s\n",p->Emaile);
printf("%d\n",p->home);
printf("%s\n",p->assort);
}
void fugai ()/*用于删除 */
{
Friends d;
FILE *fp;
fp=fopen("e:\\通讯录.txt","rb");
fread(&d,sizeof(d),1,fp);
}追问程序运行有问题!新建一组之后就停止运行了!