From 0cd4bc5019fff154ce9df7535e69d1b5ec429071 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 22 Dec 2023 13:35:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=99=E4=BA=86=E6=96=B0=E5=BB=BA=E8=81=94?= =?UTF-8?q?=E7=B3=BB=E4=BA=BA,=E4=BD=86=E6=98=AF=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E6=89=80=E6=9C=89=E8=81=94=E7=B3=BB=E4=BA=BA=E6=9C=89=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- practice_test/main.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/practice_test/main.c b/practice_test/main.c index 3061174..af5f807 100644 --- a/practice_test/main.c +++ b/practice_test/main.c @@ -1,11 +1,12 @@ #include #include #include +#include struct Contact//每个联系人的结构体 { + char name[20];//名字 char sex[10];//性别 - char name[15];//名字 char email[30];//email }; @@ -41,7 +42,30 @@ void find_mode(void)//查找联系人函数 void add_mode(void)//新增联系人函数 { - printf("add\n"); + FILE *file=fopen("contact.txt","ab");//打开文件 + if(file == NULL)//如果打开失败 + { + perror("fopen");//输出错误 + return;//结束程序 + } + struct Contact contact;//声明一个临时结构体来接收用户 + printf("请输入姓名:"); + scanf("%20s",contact.name); + printf("请输入性别:"); + scanf("%10s",contact.sex); + printf("请输入电子邮箱:"); + scanf("%30s",contact.email); + char *pick_email=NULL;//创造变量判断邮箱是否符号要求 + pick_email=strstr(contact.email,"@");//判断邮箱是否符合要求 + while (pick_email == NULL) + { + while(getchar()!='\n');//清除缓存 + printf("请输入正确格式的电子邮箱:"); + scanf("%30s",contact.email);//重新接收 + pick_email=strstr(contact.email,"@");//重新判断 + } + fwrite(&contact,sizeof(struct Contact),1,file);//写入文件 + fclose(file);//关闭文件 } void delete_mode(void)//删除联系人函数