#include
#include
#include
#include
struct persion
{
char name[10];
char tep[20];
}student;
int main()
{
int chose;
struct persion *imf_p=&student;
void input(struct persion *p);
void output(struct persion *p);
void change(struct persion *p);
void del(struct persion *p);
void add(struct persion *p);
void end();
printf("\t\t\t\t\t通讯录\n\n");
do
{
printf("\n请正确选择操作:\n");
printf("\n1.全部重输\n2.查看\n3.修改\n4.删除\n5.添加\n6.退出\n\n请选择:");
scanf("%d",&chose);
switch (chose)
{
case 1:
input(imf_p);
break;
case 2:
output(imf_p);
break;
case 3:
change(imf_p);
break;
case 4:
del(imf_p);
break;
case 5:
add(imf_p);
break;
case 6:
end();
}
}
while (1);
}
void input(struct persion *p) //输入
{
FILE *fp;
fp=fopen("imformation.dat","wb");
printf("请输入名字和电话号码:\n");
printf("\n在姓名处输入<-1>结束!\n\n");
while (1)
{
printf("姓名:");
scanf("%s",p->name);
if (!strcmp(p->name,"-1"))break;
printf("电话号码:");
scanf("%s",p->tep);
fwrite(p,sizeof(struct persion),1,fp);
}
fclose(fp);
printf("输入完成!\n");
system("pause");
system("cls");
}
void output(struct persion *p) //查看
{
FILE*fp;
fp=fopen("imformation.dat","rb");
while (1)
{
fread(p,sizeof(struct persion),1,fp);
if (feof(fp))break;
printf("姓名:%s\n",p->name);
printf("电话:%s\n",p->tep);
}
fclose(fp);
system("pause");
system("cls");
}
void change(struct persion *p) //修改
{
int i=0;
char name_change[10],tep_change[20];
FILE*fp1,*fp2;
printf("请输入要修改的人名:");
scanf("%s",name_change);
fp1=fopen("imformation.dat","rb+");
fp2=fopen("temp","wb+");
while (feof(fp1)==0)
{
fread(p,sizeof(struct persion),1,fp1);
if (!strcmp(p->name,name_change))
{
if (feof(fp1))break;
i=1;
printf("请输入新的号码:");
scanf("%s",tep_change);
strcpy(p->tep,tep_change);
}
if (feof(fp1)==0)
fwrite(p,sizeof(struct persion),1,fp2);
}
fclose(fp2);
fclose(fp1);
remove("imformation.dat");
rename("temp","imformation.dat");
if (i==1)
printf("已成功修改!\n");
if (i==0)
printf("没有搜索到文件!\n");
system("pause");
system("cls");
}
void del(struct persion *p) //删除
{
int i=0;
char name_change[10];
FILE*fp1,*fp2;
printf("请输入要删除的人名:");
scanf("%s",name_change);
fp1=fopen("imformation.dat","rb+");
fp2=fopen("temp","wb+");
while (!feof(fp1))
{
fread(p,sizeof(struct persion),1,fp1);
if (!strcmp(p->name,name_change))
{
i=1;
continue;
}
if (!feof(fp1))
fwrite(p,sizeof(struct persion),1,fp2);
}
fclose(fp2);
fclose(fp1);
remove("imformation.dat");
rename("temp","imformation.dat");
if (i==1)
printf("已完成删除操作!\n");
if (i==0)
printf("没有搜索到名字!\n");
system("pause");
system("cls");
}
void add(struct persion *p) //增加
{
FILE *fp1,*fp2;
fp1=fopen("imformation.dat","rb+");
fp2=fopen("temp","wb+");
while (!feof(fp1))
{
fread(p,sizeof(struct persion),1,fp1);
if (!feof(fp1))
fwrite(p,sizeof(struct persion),1,fp2);
}
printf("请输入要增加的名字和电话号码:\n");
printf("\n在姓名处输入<-1>结束!\n\n");
while (1)
{
printf("姓名:");
scanf("%s",p->name);
if (!strcmp(p->name,"-1"))break;
printf("电话号码:");
scanf("%s",p->tep);
fwrite(p,sizeof(struct persion),1,fp2);
}
fclose(fp1);
fclose(fp2);
remove("imformation.dat");
rename("temp","imformation.dat");
printf("输入完成!\n");
system("pause");
system("cls");
}
void end()
{
exit(0);
}