diff --git a/h1.h b/h1.h new file mode 100644 index 0000000..ce6ea59 --- /dev/null +++ b/h1.h @@ -0,0 +1,15 @@ +#include +#include +#include +#define LEN sizeof(phone) +static int count=0; +FILE *ptr1; +struct txl //通过结构体方便储存个人的各项数据 +{ + char name[10]; + char hometown[10]; + char number1[11]; + char number2[11]; + char e_mail[10]; +}phone[100]; //通过结构体数组可以储存多人的数据 + diff --git a/h2.c b/h2.c new file mode 100644 index 0000000..2294c85 --- /dev/null +++ b/h2.c @@ -0,0 +1,101 @@ +void tianjia() +{ + static int i=0; //使用static 来计算已经加入的人数。 + int j,t; + struct txl *p; + p=phone; + printf("可以开始添加\n"); + printf("电话号不能超过11个呦,亲!\n"); + printf("电子邮箱不能超过10个字符呦~~\n"); + ptr1=fopen("sto3.txt","ab+"); + if(ptr1==NULL) + { + ptr1=fopen("sto3.txt","wb+"); + if(ptr1==NULL) + { + printf("Cannot open file"); + exit(0); + } + } + fseek(ptr1,0,SEEK_END);//使文件的指针指向文件末尾,可以使其添加新的数据 + do + { + printf("请输入姓名:"); + scanf("%s",&p[i].name); printf("\n"); + printf("请输入籍贯:"); + scanf("%s",&p[i].hometown); printf("\n"); + printf("请输入电话号码1:"); + scanf("%s",&p[i].number1);printf("\n"); + printf("请输入电话号码2:"); + scanf("%s",&p[i].number2); printf("\n"); + printf("请输入电子邮箱:"); + scanf("%s",&p[i].e_mail); printf("\n"); + fwrite(&p[i],LEN,1,ptr1); + printf("完成一个添加,是否继续添加\n "); + printf("1为继续 0为退出添加\n"); + fflush(stdin); //清空缓存,让数据进入到文件中,因为如果数据大小没超过512b不会进入文件,这样使得数据强制的进入文件 + scanf("%d",&j); + i++;count++; + }while(j!=0&&count!=100); + fclose(ptr1); +} +void xiugai() +{ + FILE *ptr1; + struct txl *p; + p=phone; + char name[10],mail[10]; + int j,i,m=0; + p=phone; + ptr1=fopen("sto3.txt","rb"); + if(ptr1==NULL) + { + printf("通讯录为空\n"); + return; + } +// rewind(ptr1); + while(!feof(ptr1)) + { + if(fread(&p[m],LEN,1,ptr1)==1) + m++; + } + fclose(ptr1); + printf("请输入修改人的姓名和邮箱\n"); + printf("姓名:");scanf("%s",&name);printf("\n"); + printf("邮箱:");scanf("%s",&mail);printf("\n"); + for(i=0;i