对通讯录的操作都已经实现了,但前提是已经有一个通讯录了。那么第一次进入的时候我们需要建立一个通讯录,让前面的主程序读取。于是就有了这个“安装程序”,第一次运行通讯录前,让用户运行这个程序,将 数据,file_name,len 存入相应文件。
#include <stdio.h> #include <stdlib.h> #include <string.h> struct stu { int num; char name[20],tel[13],email[30]; char sort; char yn; struct stu *next; }; main() { FILE *in,*f_name,*ll; char fname[50],ch; struct stu stud[15],ss[15]; int i,len=0; loop:printf(" please enter the number of contacts :"); scanf("%d",&len); if(len>15) { printf(" * The number should be less than 15 "); goto loop; } puts(" * Tips: when you enter 'sort' information, 'a' means official, 'b' means personal, 'c' means commercial * "); printf(" ******************* Enter information ******************* "); for(i=0;i<len;i++) { printf(" name: "); scanf("%s",stud[i].name); printf(" tel : "); scanf("%s",stud[i].tel); printf(" sort: "); getchar(); stud[i].sort=getchar(); getchar(); printf(" email: "); scanf("%s",stud[i].email); puts(" "); } if((in=fopen("D:\telbook.dat","wb"))==NULL) { puts("cannot open the file"); exit (0); } for(i=0;i<len;i++) fwrite(&stud[i],sizeof(struct stu),1,in); fclose(in); if((f_name=fopen("D:\file_name.dat","wb"))==NULL) { puts("cannot open the file"); exit (0); } rewind(f_name); fputs("D:\telbook.dat",f_name); // note filename fclose(f_name); if((in=fopen("D:\len.dat","wb"))==NULL) { puts("cannot open the file"); exit(0); } rewind(in); fputc(len,in); fclose(in); //check puts(" Check"); if((in=fopen("D:\telbook.dat","rb"))==NULL) { puts("cannot open the file"); exit (0); } for(i=0;i<len;i++) fread(&ss[i],sizeof(struct stu),1,in); fclose(in); if((f_name=fopen("D:\file_name.dat","rb"))==NULL) { puts("cannot open the file"); exit (0); } fgets(fname,49,f_name); // note filename fclose(f_name); printf(" file name : %s ",fname); for(i=0;i<len;i++) { printf(" name: %s ",ss[i].name); printf(" tel : %s ",ss[i].tel); printf(" sort: %c",ss[i].sort); printf(" email: %s",ss[i].email); puts(" "); } if((ll=fopen("D:\len.dat","rb"))==NULL) { puts("cannot open the file len"); exit(0); } rewind(ll); ch=fgetc(ll); fclose(ll); printf(" saved len : %d ",ch); //check end }