#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define login 14062120
FILE *fp;
int n = 0;
void clean()
{
char getchar = '0';
while (getchar != '\n')
scanf_s("%c", &getchar);
return;
}
struct tongxunlu
{
char xingming[100];
char zhiwu[100];
char danwei[100];
char gudingdianhua[100];
char shouji[100];
char email[100];
char QQ[100];
char MSN[100];
}txl[200];
void readfile()
{
void tianjia();
void xiugai();
void chazhao();
void shanchu();
int ZCM, choice;
char ch;
FILE *fp;
if (( fopen_s(&fp,"c:\\通讯录.txt", "rb+")) == NULL) // rb+ 读写,为读写方式打开已存在的二进制文件
{
printf("通讯录不存在,请输入注册码");
scanf_s("%d", &ZCM);
if (ZCM == login)
{
if ((fopen_s(&fp,"c:\\通讯录.txt", "w")) == NULL)
{
printf("\n\t\t建立失败");
exit(0);
}
}
else
{
printf("注册码错误");
exit(0);
}
}
printf("\n\t-----------------欢迎使用通讯录管理系统------------------");
printf("\n\t 通讯录文件已建立 ");
printf("\n\t 按任意键进入主菜单 ");
printf("\n\t---------------------------------------------------------");
ch = getchar();
while (1)
{
n = fread(txl, sizeof(tongxunlu), 200, fp); //最大可读200个txl
fclose(fp);
printf("\n\t---------------------------------------------------------");
printf("\n\t- 1.添加联系人 2.查找联系人 -");
printf("\n\t- 3.删除联系人 4.修改联系人 -");
printf("\n\t- 5.保存并退出 -");
printf("\n\t- 欢迎%d 现在有联系人%d人 -", login, n);
printf("\n\t---------------------------------------------------------\n");
printf("请输入1,2,3,4,5:");
scanf_s("%d", &choice);
switch (choice)
{
case 1: tianjia(); break;
case 2: chazhao(); break;
/*case 3: shanchu(); break;
case 4: xiugai(); break;
case 5: exit(0); break;*/
}
}
}
void tianjia() //添加联系人
{
int i;
char a;
do
{
fopen_s(&fp,"c:\\通讯录.txt", "ab");
printf("\n\t\t---------------请输入联系人信息-----------------");
printf("\n\t\t输入联系人姓名:");
scanf_s("%s", txl[0].xingming);
printf("\n\t\t输入联系人职务:");
scanf_s("%s", txl[0].zhiwu);
printf("\n\t\t输入联系人单位:");
scanf_s("%s", txl[0].danwei);
printf("\n\t\t输入联系人固定电话:");
scanf_s("%s", txl[0].gudingdianhua);
printf("\n\t\t输入联系人手机:");
scanf_s("%s", txl[0].shouji);
printf("\n\t\t输入联系人邮箱:");
scanf_s("%s", txl[0].email);
printf("\n\t\t输入联系人QQ:");
scanf_s("%s", txl[0].QQ);
printf("\n\t\t输入联系人MSN:");
scanf_s("%s", txl[0].MSN);
fwrite(txl, sizeof(struct tongxunlu), 1, fp);
printf("\n\t\t是否继续添加联系人?(y/n):");
fclose(fp);
clean();
a = getchar();
} while (a == 'y');
return;
}
void chazhao() //查找联系人
{
fopen_s(&fp,"c:\\通讯录.txt", "rb+");
int i = 1;
char name[100];
printf("\n\t\t---------------请输入要查找联系人信息-----------------");
printf("\n\t\t输入查找的联系人的姓名:");
scanf_s("%s", name);
while (!feof(fp))
{
fread(txl, sizeof(tongxunlu), 1, fp);
if (strstr(txl[0].xingming, name))
{
i = 0;
printf("姓名:%s", txl[0].xingming);
printf("职务:%s", txl[0].zhiwu);
printf("单位:%s", txl[0].danwei);
printf("固定电话:%s", txl[0].gudingdianhua);
printf("手机:%s", txl[0].shouji);
printf("Email:%s", txl[0].email);
printf("QQ:%s", txl[0].QQ);
printf("MSN:%s", txl[0].MSN);
}
}
if (i)
printf("\n\n\t\t\t\t查无此人!!\n\n");
return;
}
void main()
{
void readfile();
readfile();
}