#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct person //创造结构体
{
char name[8];
char tel[15];
};
char filename[20];
FILE *fp; //定义一个指向文件的指针变量fp
void creat(); //创建一个通讯录
void output();
void search();
void append();
void modify();
void dele();
void main()
{
int m;
char k;
printf("\n请输入你要打开的通讯录文件名:");
gets(filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n当前没有此通讯录文件,现在是否执行创建(Y/N)?:");
scanf("%c",&k);
if(k!='Y'&&k!='N'&&k!='y'&&k!='n')
{
printf("\n输入有误,请再次输入一个值:");
scanf("%c",&k);
}
else if(k=='Y'||k=='y')
{
creat();
}
else if(k=='N'||k=='n')
{
printf("\n由于你选择了退出,现在程序即将关闭!\n");
exit(0); //所在头文件为stdlib.h
//功 能: 关闭所有文件,终止正在执行的进程。
}
}
else
{
printf("\n你要打开的通讯录文件%s已存在,现在可直接对其操作:\n",filename);
printf("通讯录文件中现已有的通讯信息:\n");
output();
printf("\n请选择根据提示选择1~5对通讯录文件进行操作。\n");
}
while (1)
{
printf("\n\n添加,请按1"); printf("\n查找,请按2");
printf("\n修改,请按3");
printf("\n删除,请按4");
printf("\n输出,请按5");
printf("\n退出,请按0\n");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{ case 0:exit(0);
case 1: append();break;
case 2: search();break;
case 3: modify();break;
case 4: dele();break;
case 5:output();break;
}
printf("\n\n操作完毕,请再次选择!");
}
else printf("\n\n操作错误,请再次选择!:");
}
}
/**********************************************************************/
void creat()
{
struct person one;printf("\n请输入通讯簿文件名:");
scanf("%s",filename);
if ((fp=fopen(filename,"w"))==NULL)
{
printf("\n不能建立通讯薄!");
exit(0);
}
fprintf(fp,"%-10s%-20s%\n","姓名","电话号码"); //用格式化的方式读写文件
printf("\n请输入姓名:\n");
scanf("%s",one.name);
while (strcmp(one.name,"0")) //用strcmp函数来处理,与"0"比较,
//如果输入不为空,就是while(1)一直进行
//直到输入为空跳出
{
printf("请输入电话号码:\n");
scanf("%s",one.tel);
fprintf(fp,"%-10s%-20s%\n",one.name,one.tel);
printf("请再输入另一个人的姓名,若想要结束,请输入0\n");
scanf("%s",one.name);
}
fclose(fp);
}
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打开通讯薄!");
exit(0);
}
while (!feof(fp)) // feof(FILE*fp)输入输出函数,检查文件是否结束
//如果结束,则返回非零值,否则返回0
{
fscanf(fp,"%s%s\n",one.name,one.tel);
printf("%-10s%-20s%\n",one.name,one.tel);
}
fclose(fp);
}
void append()
{
struct person one;
if ((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打开通讯薄!");
exit(0);
}
printf("\n请输入添加的姓名\n");
scanf("%s",one.name);
printf("请输入电话号码:\n");
scanf("%s",one.tel);
printf("%-10s%-20s%\n",one.name,one.tel);
fprintf(fp,"%-10s%-20s%\n",one.name,one.tel);
fclose(fp);
}
void search()
{
int flag=0;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打开通讯薄!");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s%s\n",one.name,one.tel);
if (!strcmp(namekey,one.name))
{
printf("\n\n已查到,记录为:");
printf("\n%-10s%-20s%",one.name,one.tel);
flag=1;
}
}
if(!flag)
printf("\n\n对不起,通讯薄中没有此人的记录。");
fclose(fp);
}
void modify()
{
int flag=0;
long offset;
char namekey[8];
struct person one;printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯薄!");
exit(0);
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s\n",one.name,one.tel);
if(!strcmp(namekey,one.name))
{
flag=1;break;
}
}
if(flag)
{
printf("\n已查到,记录为");
printf("\n%-10s%-20s%",one.name,one.tel);
while (1)
{
printf("\n\n修改姓名,请按2");
printf("\n修改电话,请按3");
printf("\n退出,请按6\n");
scanf("%d",&flag);
if (flag==2)
{
printf("请输入新的姓名\n");
scanf("%s",one.name);
printf("修改完毕,请指示!:\n");
}
if (flag==3) {printf("请输入新的电话\n");
scanf("%s",one.tel);
printf("修改完毕,请指示!:\n");
}
if (flag==6) break;
}
fseek(fp,offset,0);
fprintf(fp,"%-10s%-20s%\n",one.name,one.tel);
printf("\n%-10s%-20s%",one.name,one.tel);
}
else printf("不存在指定的名字!\n");
fclose(fp);
}
void dele()
{
int m,flag=0;
long offset;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("\n不能打开通讯簿!");
exit(0);
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s\n",one.name,one.tel);
if(!strcmp(namekey,one.name))
{
flag=1;
break;
}
}
if(flag)
{
printf("\n已查到,记录为");
printf("\n%-10s%-20s%",one.name,one.tel);
printf("\n确定要删除,按1;不删除,按0:");
scanf("%d",&m);
if (m)
{
fseek(fp,offset,SEEK_SET);
fprintf(fp,"%-10s%-20s\n","",""); }
}
else
printf("\n对不起,通讯簿中没有此人的记录。");
fclose(fp);
}