/*编程建立一通讯簿,存放有姓名、电话号码、住址,然后对通信簿进行查找、添加、修改及删除。*/
#include<stdio.h>
struct person
{
char name[15];
char tel[15];
char addr[30];
char email[30];
};
char filename[20];
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void delet();
main()
{
int m;
creat();
while(1)
{
printf("\n\nadd, Press 1");
printf("\nsearch, Press 2");
printf("\nmodify Press 3");
printf("\ndelete, Press 4");
printf("\nshow, Press 5");
printf("\nexit, Press 0\n");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: delet();
break;
case 5: output();
break;
case 0: exit();
}
printf("\n\nfinish ,please choose again!");
}
else
printf("\n\nwrong, please choose again!");
}
}
void creat()
{
struct person one;
long s1;
printf("\nPlease input the address booke name .\n");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("\nCan't buide address book.");
exit();
fprintf(fp,"%-15s%-20s%-30s%-30s","name","telephone","address","email");
}
}
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\nCan't open address book.\n ");
exit();
}
printf("\n\n%38s\n","tong xun bu");
while(!feof(fp))
{
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.addr,one.email);
printf("%-15s%-20s%-20s%-25s",one.name,one.tel,one.addr,one.email);
}
fclose(fp);
}
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("\nCan't open address book.");
exit();
}
printf("\nplease input name,telephonenumber,address and email.\n");
scanf("%s%s%s%s",one.name,one.tel,one.addr,one.email);
fprintf(fp,"%-15s%-20s%-30s%-30s\n",one.name,one.tel,one.addr,one.email);
fclose(fp);
}
void search()
{
int k=0;
char namekey[15];
struct person one;
printf("\nplease input name.\n");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\nCan't open address book.");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.addr,one.email);
if(!strcmp(namekey,one.name))
{
printf("\n\nHave found , the record is:");
printf("\n%-15s%-20s%-30s%-30s",one.name,one.tel,one.addr,one.email);
k=1;
}
}
if(!k)
printf("\n\nsorry,don't have this person's record. n");
fclose(fp);
}
void modify()
{
int m,k=0;
long offset;
char namekey[8];
struct person one;
printf("\nplease input name.\n");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\nCan't open address book.");
exit();
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.addr,one.email);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\nHave found the record is:");
printf("\n%-15s%-20s%-30s%-30s",one.name,one.tel,one.addr,one.email);
printf("\nplease input name,telephonenumber,address and email.");
scanf("%s%s%s%s",one.name,one.tel,one.addr,one.email);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp)); /*??*/
fprintf(fp,"%-10s%-20s%-30s%-50s\n",one.name,one.tel,one.addr,one.email);
}
else
printf("\nsorry,don't have this person's record.");
fclose(fp);
}
void delet()
{
int m,k=0,flag;
long offset1,offset2;
char namekey[8], valid[4];
struct person one;
printf("\nplease input name.\n ");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\nCan't open address book.");
exit();
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s%s\n",one.name,one.tel,one.addr,one.email);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("\nHave found the record is:");
printf("\n%-10s%-18s%-30s%-50s",one.name,one.tel,one.addr,one.email);
printf("\n Make sure you want to delete press 1,cancel press 0.");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);/*????*/
fprintf(fp,"%-15s%-20s%-30s%-30s\n","","","","");
}
}
else
printf("\nsorry,don't have this person's record.");
fclose(fp);
}