#include<stdio.h>
#include<string.h>
struct book{
char name[30];
char phone[30];
};
void add(){
FILE *fp;
char flag='1';
struct book bk[700];
int i=0,c;
if((fp=fopen("cc","ab+"))==NULL)
{
printf("\nERROR oening customet file");
}
printf("\n要添加几条记录(填整数)?");
scanf("%d",&c);
for(i=0;i<c;i++)
{
printf("\n输入姓名:");
scanf("%s",bk[i].name);
printf("\n输入电话号码:");
scanf("%s",bk[i].phone);
}
for(i=0;i<c;i++)
{
fwrite(&bk[i],sizeof(struct book),1,fp);
}
fclose(fp);
}
void read(){
FILE *fp;
struct book bk;
//char ch;
if((fp=fopen("cc","rb"))==NULL){
printf("\nERROR oening customet file");
}
printf("\n姓名:\t电话:");
while((fread(&bk,sizeof(struct book),1,fp))==1){
printf("\n%s\t%s\t",bk.name,bk.phone);}
fclose(fp);
}
void search(){
FILE *fp;
struct book bk;
int i,sf,s;
char sear[100];
//char *loc;
if((fp=fopen("cc","r+w"))==NULL){
printf("\nERROR oening customet file");
}
cc:
printf("\n1姓名\n2电话\n你要按什么来找?");
scanf("%d",&s);
if(s==1)
{
printf("\n请输入要查的姓名:");
scanf("%s",&sear);
}else if(s==2){
printf("\n请输入要查的电话:");
scanf("%s",&sear);
}
printf("\n姓名:\t电话:");
i=0;
rewind(fp);
while((fread(&bk,sizeof(struct book),1,fp))==1)
{
if(s==1)
{ //loc=strchr(bk.name,sear);
//if(loc!=NULL)
if(!strcmp(bk.name,sear))
{
printf("\n%s\t%s\t",bk.name,bk.phone);
i++;
}
}
if(s==2)
{
if(!strcmp(bk.phone,sear))
{
printf("\n%s\t%s\t",bk.name,bk.phone);
i++;
}
}
}
if(i==0){
printf("\n对不起!没有你要找的数据");
}else{
printf("\n系统共找到%d条数据.",i);
}
printf("继续查找(1/0)?");
scanf("%d",&sf);
if(sf==1)
goto cc;
fclose(fp);
}
void del()
{
FILE *fp;
struct book bk[700];
struct book bkt;
int i,sf,s,ii=0,it,cv;
char sear[100];
//char *loc;
if((fp=fopen("cc","rb"))==NULL){
printf("\nERROR oening customet file");
}
while((fread(&bkt,sizeof(struct book),1,fp))==1)
{
strcpy(bk[ii].name,bkt.name);
strcpy(bk[ii].phone,bkt.phone);
ii++;
}
fclose(fp);
it=ii;
cc:
printf("\n1姓名\n2电话\n你要查找那一条记录来删除?");
scanf("%d",&s);
if(s==1)
{
printf("\n请输入要查的姓名:");
scanf("%s",&sear);
}else if(s==2){
printf("\n请输入要查的电话:");
scanf("%s",&sear);
}
printf("\n姓名:\t电话:");
i=0;
ii=0;
for(;ii<it;ii++)
{
if(s==1)
{ //loc=strchr(bk.name,sear);
//if(loc!=NULL)
if(!strcmp(bk[ii].name,sear))
{
printf("\n%s\t%s\t",bk[ii].name,bk[ii].phone);
i++;
printf("确定要删除吗(1/0)?");
scanf("%d",&cv);
for(;ii<it;ii++)
{
if(ii==it-1)
break;
strcpy(bk[ii].name,bk[ii+1].name);
strcpy(bk[ii].phone,bk[ii+1].phone);
}
}
}
if(s==2)
{
if(!strcmp(bk[ii].phone,sear))
{
printf("\n%s\t%s\t",bk[ii].name,bk[ii].phone);
i++;
printf("/n确定要删除吗(1/0)?");
scanf("%d",&cv);
if(cv==1)
for(;ii<it;ii++)
{
if(ii==it-1)
break;
strcpy(bk[ii].name,bk[ii+1].name);
strcpy(bk[ii].phone,bk[ii+1].phone);
}
}
}
}
if(i==0){
printf("\n对不起!没有你要找的数据");
}else
{ if((fp=fopen("cc","wb"))==NULL)
{
printf("\nERROR oening customet file");
}
for(ii=0;ii<it-1;ii++)
fwrite(&bk[ii],sizeof(struct book),1,fp);
printf("\n该记录已被删除!.");
fclose(fp);
}
printf("继续删除(1/0)?");
scanf("%d",&sf);
if(sf==1)
goto cc;
}
void change()
{ FILE *fp;
struct book bk[700];
struct book bkt;
int i,sf,s,ii=0,it;
char sear[100];
char temp[100];
//char *loc;
if((fp=fopen("cc","rb"))==NULL){
printf("\nERROR oening customet file");
}
while((fread(&bkt,sizeof(struct book),1,fp))==1)
{
strcpy(bk[ii].name,bkt.name);
strcpy(bk[ii].phone,bkt.phone);
ii++;
}
fclose(fp);
it=ii;
cc:
printf("\n1姓名\n2电话\n你要修改什么?");
scanf("%d",&s);
if(s==1)
{
printf("\n请输入要查的姓名:");
scanf("%s",&sear);
}else if(s==2){
printf("\n请输入要查的电话:");
scanf("%s",&sear);
}
printf("\n姓名:\t电话:");
i=0;
ii=0;
//while((fread(&bk,sizeof(struct book),1,fp))==1)
for(;ii<it;ii++)
{
if(s==1)
{ //loc=strchr(bk.name,sear);
//if(loc!=NULL)
if(!strcmp(bk[ii].name,sear))
{
printf("\n%s\t%s\t",bk[ii].name,bk[ii].phone);
i++;
printf("\n请输入修改后的姓名:");
scanf("%s",&temp);
strcpy(bk[ii].name,temp);
}
}
if(s==2)
{
if(!strcmp(bk[ii].phone,sear))
{
printf("\n%s\t%s\t",bk[ii].name,bk[ii].phone);
i++;
printf("\n请输入修改后的电话号码:");
scanf("%s",&temp);
strcpy(bk[ii].phone,temp);
}
}
}
if(i==0){
printf("\n对不起!没有你要找的数据");
}else
{ if((fp=fopen("cc","wb"))==NULL)
{
printf("\nERROR oening customet file");
}
for(ii=0;ii<it;ii++)
fwrite(&bk[ii],sizeof(struct book),1,fp);
printf("\n修改完成!.");
fclose(fp);
}
printf("继续修改(1/0)?");
scanf("%d",&sf);
if(sf==1)
goto cc;
}
void main(){
int ch;
FILE *fp;
if((fp=fopen("cc","ab+"))==NULL){
printf("\nERROR oening customet file");
}
fclose(fp);
do{
printf("\n\n \2\2\2\2\2\2\2\2\2\2\2\2\2\2电★话★查★询★系★统\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\2\n");
printf("\n1添加\n2读取所有\n3查找\n4删除\n5修改\n6退出\n进行那个操作(1,2,3,4,5,6)?");
scanf("%d",&ch);
if(ch==1)
add();
if(ch==2)
read();
if(ch==3)
search();
if(ch==4)
del();
if(ch==5)
change();
}while(ch!=6);
}