|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <string.h>
void creat();//创建函数
void append();//添加记录函数
void serch();//查找记录函数
void output();//输出记录函数
FILE *fp;
struct person
{
char name[8];
char tel[12];
char addr[50];
};char filename[100];
void creat()
{
struct person one;
printf("please input filename");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("error");
exit(1);
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","住址");
printf("\n请输入姓名,电话,住址(0键结束)");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%50s\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}
void append()
{
struct person one;
if ((fp=fopen(filename,"a"))==NULL)
{
printf("error");
exit(1);
}
printf("\n请输入姓名,电话,住址(0键结束)");
scanf("%s",one.name);
while(strcmp(one.name,"0"))
{
scanf("%s%s",one.tel,one.addr);
fprintf(fp,"%-10s%-20s%50s\n",one.name,one.tel,one.addr);
scanf("%s",one.name);
}
fclose(fp);
}
void serch()
{
char a[8];
int k=0;
struct person one;
printf("plesae input person of name");
scanf("%s",a);
if ((fp=fopen(filename,"r"))==NULL)
{
printf("error");
exit(1);
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(one.name,a))
{
printf("\n%-10s%-20s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if (k=0)
{
printf("记录里没有此人");
fclose(fp);
}
void output()
{
struct person one;
if ((fp=fopen(filename,"r"))==NULL)
{
printf("error");
exit(1);
}
while(!feof(fp))
{
printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}
void main()
{
int s=0;
printf(" **************欢迎进入通讯录************************\n\n");
printf(" 1.添加记录\n");
printf(" 2.创建记录\n");
printf(" 3.输出记录\n");
printf(" 4.查找记录\n");
printf(" 5.退出系统\n")
printf("\n\n 请按数字键选择:");
scanf("%d",&s);
if(s>0&&s<5)
{
switch(s)
{
case 1: append(); break;
case 2: creat(); break;
case 3: output(); break;
case 4: serch(); break;
case 5: exit(); break;
}
}
else
{
printf("error");
exit(1);
}
}
|
|