#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void add();
void name();
void dz();
void xm();
void del();
char flag='y';
int i,j,l,n;
struct tels
{
char name[50];
int dh;
char dz[20];
};
void main()
{
printf("/*****************************电话号码存储系统*******************************/\n");
printf(" /*****************************制作:刘山*******************************/\n");
while(n!=6)
{
printf("1.增加电话\n2.按姓名修改纪录\n3.按地址查询\n4.按姓名查询\n5.删除\n6.退出\n请选择:");
scanf("%d",&n);
if(n==1)
add();//增加电话
else if(n==2)
name();//按姓名修改
else if(n==3)
dz();//按地址查询
else if(n==4)
xm();//按姓名查询
else if(n==5)
del();
}
}
void add()
{
char flag='y';
struct tels tel;
FILE *fp;
if((fp=fopen("d:/c.txt","a+b"))==NULL)
{
exit(0);
}
while(flag=='y')
{
printf("请输入姓名:");
scanf("%s",tel.name);
printf("请输入电话:");
scanf("%d",&tel.dh);
printf("请输入地址:");
scanf("%s",tel.dz);
fwrite(&tel,sizeof(struct tels),1,fp);
printf("是否继续y/n:\n");
scanf(" %c",&flag);
}
fclose(fp);
}
void name()
{
FILE *fp;
struct tels tel;
char flag='y',find;
char name[30];
if((fp=fopen("d:/c.txt","r+w"))==NULL)
printf("\n\tThe system can't open the file!!");
while(flag=='y')
{
printf("请输入您要修改的用户名");
scanf("%s",name);
find='n';
rewind(fp);
while((fread(&tel,sizeof(tel),1,fp))==1&&find=='n')
{
if(strcmp(tel.name,name)==0)
{
find='y';
break;
}
}
if(find=='y')
{
printf("姓名%s\n",tel.name);
printf("地址%s\n",tel.dz);
printf("电话:%d\n",tel.dh);
printf("请输入姓名");
scanf("%s",tel.name);
printf("请输入地址");
scanf("%s",tel.dz);
printf("请输入电话:");
scanf("%d",&tel.dh);
fseek(fp,-(long)(sizeof(struct tels)),1);/*文件标记向下*/
fwrite(&tel,sizeof(tel),1,fp);
}
printf("是否继续:");/*提示是否继续修改另一已存用户信息*/
scanf(" %c",&flag);
}
fclose(fp); /*关闭文件*/
}
void dz()
{
char flay='y';
char find='n';
char dz[30];
FILE *fp;
struct tels tel;
if((fp=fopen("d:/c.txt","r+b"))==NULL)
printf("\n\tThe system can't open the file!!");
while(flay=='y')
{
printf("请输入您要查的地址:");
scanf("%s",dz);
rewind(fp);
while((fread(&tel,sizeof(tel),1,fp))==1&&find=='n')
{
if(strcmp(dz,tel.dz)==0)
{
find='y';
break;
}
}
if(find=='y')
{
printf("姓名%s\n",tel.name);
printf("地址%s\n",tel.dz);
printf("电话:%d\n",tel.dh);
}
printf("是否继续y/n\n");
scanf(" %c",&flay);
}
fclose(fp);
}
void xm()//姓名查询
{
char flay='y';
char find='n';
char xm[30];
FILE *fp;
struct tels tel;
if((fp=fopen("d:/c.txt","r+b"))==NULL)
printf("\n\tThe system can't open the file!!");
while(flay=='y')
{
printf("请输入您要查的姓名:");
scanf("%s",xm);
rewind(fp);
while((fread(&tel,sizeof(tel),1,fp))==1&&find=='n')
{
if(strcmp(xm,tel.name)==0)
{
find='y';
break;
}
}
if(find=='y')
{
printf("姓名%s\n",tel.name);
printf("地址%s\n",tel.dz);
printf("电话:%d\n",tel.dh);
}
printf("是否继续y/n\n");
scanf(" %c",&flay);
}
fclose(fp);
}
void del()
{
struct tels tel;
FILE *fp;
if((fp=fopen("d:/c.txt","r+w"))==NULL)
printf("打开文件出错");
char flag='y';
char find='n';
char xm[30];
char n;
while(flag=='y')
{
printf("请输入姓名:");
scanf("%s",xm);
rewind(fp);
while((fread(&tel,sizeof(tel),1,fp))==1&&find=='n')
{
if(strcmp(xm,tel.name)==0)
{
find='y';
break;
}
}
if(find=='y')
{
printf("姓名%s\n",tel.name);
printf("地址%s\n",tel.dz);
printf("电话:%d\n",tel.dh);
printf("是否确定删除y/n:");
scanf(" %c",&n);
if(n=='y')
strcpy(tel.name,"");
strcpy(tel.dz,"");
tel.dh=0;
fseek(fp,-(long)(sizeof(struct tels)),1);
fwrite(&tel,sizeof(tel),1,fp);
printf("删除成功\n");
}
printf("是否继续删除y/n:\n");
scanf(" %c",&flag);
}
fclose(fp);
}
#include<string.h>
#include<stdlib.h>
void add();
void name();
void dz();
void xm();
void del();
char flag='y';
int i,j,l,n;
struct tels
{
char name[50];
int dh;
char dz[20];
};
void main()
{
printf("/*****************************电话号码存储系统*******************************/\n");
printf(" /*****************************制作:刘山*******************************/\n");
while(n!=6)
{
printf("1.增加电话\n2.按姓名修改纪录\n3.按地址查询\n4.按姓名查询\n5.删除\n6.退出\n请选择:");
scanf("%d",&n);
if(n==1)
add();//增加电话
else if(n==2)
name();//按姓名修改
else if(n==3)
dz();//按地址查询
else if(n==4)
xm();//按姓名查询
else if(n==5)
del();
}
}
void add()
{
char flag='y';
struct tels tel;
FILE *fp;
if((fp=fopen("d:/c.txt","a+b"))==NULL)
{
exit(0);
}
while(flag=='y')
{
printf("请输入姓名:");
scanf("%s",tel.name);
printf("请输入电话:");
scanf("%d",&tel.dh);
printf("请输入地址:");
scanf("%s",tel.dz);
fwrite(&tel,sizeof(struct tels),1,fp);
printf("是否继续y/n:\n");
scanf(" %c",&flag);
}
fclose(fp);
}
void name()
{
FILE *fp;
struct tels tel;
char flag='y',find;
char name[30];
if((fp=fopen("d:/c.txt","r+w"))==NULL)
printf("\n\tThe system can't open the file!!");
while(flag=='y')
{
printf("请输入您要修改的用户名");
scanf("%s",name);
find='n';
rewind(fp);
while((fread(&tel,sizeof(tel),1,fp))==1&&find=='n')
{
if(strcmp(tel.name,name)==0)
{
find='y';
break;
}
}
if(find=='y')
{
printf("姓名%s\n",tel.name);
printf("地址%s\n",tel.dz);
printf("电话:%d\n",tel.dh);
printf("请输入姓名");
scanf("%s",tel.name);
printf("请输入地址");
scanf("%s",tel.dz);
printf("请输入电话:");
scanf("%d",&tel.dh);
fseek(fp,-(long)(sizeof(struct tels)),1);/*文件标记向下*/
fwrite(&tel,sizeof(tel),1,fp);
}
printf("是否继续:");/*提示是否继续修改另一已存用户信息*/
scanf(" %c",&flag);
}
fclose(fp); /*关闭文件*/
}
void dz()
{
char flay='y';
char find='n';
char dz[30];
FILE *fp;
struct tels tel;
if((fp=fopen("d:/c.txt","r+b"))==NULL)
printf("\n\tThe system can't open the file!!");
while(flay=='y')
{
printf("请输入您要查的地址:");
scanf("%s",dz);
rewind(fp);
while((fread(&tel,sizeof(tel),1,fp))==1&&find=='n')
{
if(strcmp(dz,tel.dz)==0)
{
find='y';
break;
}
}
if(find=='y')
{
printf("姓名%s\n",tel.name);
printf("地址%s\n",tel.dz);
printf("电话:%d\n",tel.dh);
}
printf("是否继续y/n\n");
scanf(" %c",&flay);
}
fclose(fp);
}
void xm()//姓名查询
{
char flay='y';
char find='n';
char xm[30];
FILE *fp;
struct tels tel;
if((fp=fopen("d:/c.txt","r+b"))==NULL)
printf("\n\tThe system can't open the file!!");
while(flay=='y')
{
printf("请输入您要查的姓名:");
scanf("%s",xm);
rewind(fp);
while((fread(&tel,sizeof(tel),1,fp))==1&&find=='n')
{
if(strcmp(xm,tel.name)==0)
{
find='y';
break;
}
}
if(find=='y')
{
printf("姓名%s\n",tel.name);
printf("地址%s\n",tel.dz);
printf("电话:%d\n",tel.dh);
}
printf("是否继续y/n\n");
scanf(" %c",&flay);
}
fclose(fp);
}
void del()
{
struct tels tel;
FILE *fp;
if((fp=fopen("d:/c.txt","r+w"))==NULL)
printf("打开文件出错");
char flag='y';
char find='n';
char xm[30];
char n;
while(flag=='y')
{
printf("请输入姓名:");
scanf("%s",xm);
rewind(fp);
while((fread(&tel,sizeof(tel),1,fp))==1&&find=='n')
{
if(strcmp(xm,tel.name)==0)
{
find='y';
break;
}
}
if(find=='y')
{
printf("姓名%s\n",tel.name);
printf("地址%s\n",tel.dz);
printf("电话:%d\n",tel.dh);
printf("是否确定删除y/n:");
scanf(" %c",&n);
if(n=='y')
strcpy(tel.name,"");
strcpy(tel.dz,"");
tel.dh=0;
fseek(fp,-(long)(sizeof(struct tels)),1);
fwrite(&tel,sizeof(tel),1,fp);
printf("删除成功\n");
}
printf("是否继续删除y/n:\n");
scanf(" %c",&flag);
}
fclose(fp);
}