其它答案:
/* HELLO.C -- Hello, world */
#include<stdio.h>
struct person
{
char name[8];
char tel[15];
char addr[50];
};
char filename[20]="E:\txl.txt";
FILE *fp;
void creat();
void output();
void search();
void append();
void modify();
void delete();
main( )
{
int m;
creat();
while(1)
{
printf("
*********欢迎使用通讯录信息管理系统*********
");
printf("
添加,请按1");
printf("
查找,请按2");
printf("
修改,请按3");
printf("
删除,请按4");
printf("
输出,请按5");
printf("
退出,请按0
");
printf("
********************************************
");
printf("Please select(0--5):");
scanf("%d",&m);
if(m>=0&&m<=5)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: modify();
break;
case 4: delete();
break;
case 5: output();
break;
case 0: exit();
}
printf("
操作完毕,请再次选择!");
}
else
printf("
选择错误,请再次选择!");
}
}
/*输入模块creat( ): 创建通讯录的子函数。*/
void creat()
{
struct person one;
long s1;
/*printf("
请输入通讯录文件名:");
scanf("%s",filename); */
if((fp=fopen(filename,"at+"))==NULL)
{
printf("
不能建立通讯录!");
exit();
}
fprintf(fp,"%-10s%-20s%-50s
","姓名","电话号码","住址");
printf("
请输入姓名、电话号码及住址(以0结束)
");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s
",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}
/*输出模块output( ):输出通讯录中联系人通讯信息的子函数*/
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("
不能打开通讯录!");
exit();
}
printf("
%20s
","通 讯 录");
while(!feof(fp))
{
fscanf(fp,"%s%s%s
",one.name,one.tel,one.addr);
printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}
/*添加模块append( ):向通讯录中添加某人通讯信息的子函数*/
void append()
{
struct person one;
if((fp=fopen(filename,"a"))==NULL)
{
printf("
不能打开通讯录!");
exit();
}
printf("
请输入添加的姓名、电话号码及住址
");
scanf("%s%s%s",one.name,one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s
",one.name,one.tel,one.addr);
fclose(fp);
}
/*查找模块search( ):在通讯录中查找某人通讯信息的子函数*/
void search()
{
int k=0;
char namekey[8];
struct person one;
printf("
请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("
不能打开通讯录!");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s
",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("
已查到,记录为:");
printf("
%-10s%-18s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("
对不起,通讯簿中没有此人的记录。");
fclose(fp);
}
/*修改模块modify( ):在通讯录中修改某人通讯信息的子函数*/
void modify()
{
int m,k=0;
long offset;
char namekey[8];
struct person one;
printf("
请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("
不能打开通讯录!");
exit();
}
while(!feof(fp))
{
offset=ftell(fp);
fscanf(fp,"%s%s%s
",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
}
}
if(k)
{
printf("
已查到,记录为:");
printf("
%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("
请输入新姓名、电话号码及住址:");
scanf("%s%s%s",one.name,one.tel,one.addr);
fseek(fp,offset,SEEK_SET);
printf("%ld",ftell(fp));
fprintf(fp,"%-10s%-20s%-50s
",one.name,one.tel,one.addr);
}
else
printf("
对不起,通讯录中没有此人的记录。");
fclose(fp);
}
/*删除模块delete( ):在通讯录中删除某人通讯信息的子函数*/
void delete()
{
int m,k=0,flag;
long offset1,offset2;
char namekey[8], valid[4];
struct person one;
printf("
请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"r+"))==NULL)
{
printf("
不能打开通讯录!");
exit();
}
while(!feof(fp))
{
offset1=ftell(fp);
fscanf(fp,"%s%s%s
",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
k=1;
break;
if(k)
{
printf("
已查到,记录为");
printf("
%-10s%-18s%-50s",one.name,one.tel,one.addr);
printf("
确实要删除,按1;不删除,按0:");
scanf("%d",&m);
if(m)
{
fseek(fp,offset1,SEEK_SET);
fprintf(fp,"%-10s%-20s%-50s
","","","");
}
else
printf("
对不起,通讯录中没有此人的记录。");
fclose(fp);
}
}
}
}
<PIXTEL_MMI_EBOOK_2005>1###########################################################</PIXTEL_MMI_EBOOK
回答者:117.42.127.* 日期:2010/2/25 20:14:55
[首页]
[上页]
1
[下页]
[尾页]
每页20条 1条/1页 (第1页)