#include #include #include #include #include #define LEN sizeof(struct addritem) #define FORMAT "%-15s%-20s%-10s%-15s%-30s%\n"//规定输出格式 #define DATA addrinfo[i].name,addrinfo[i].occu,addrinfo[i].tel,addrinfo[i].email,addrinfo[i].address//结构体数组元素中所有成员,方便引用 struct addritem { char name[10]; char occu[10]; char address[35]; char tel[15]; char email[25]; }; struct addritem addrinfo[100]; void input(); void search(); void update(); void del(); void display(); void sort(); void menu(); void menu() { system("cls"); printf("\n\n\n\n\n"); printf("\t\t|----------contact--------|\n"); printf("\t\t|0.exit |\n"); printf("\t\t|1.input record |\n"); printf("\t\t|2.search record |\n"); printf("\t\t|3.update record |\n"); printf("\t\t|4.delete record |\n"); printf("\t\t|5.sort |\n"); printf("\t\t|6.display |\n"); printf("\t\t|-------------------------|\n\n"); printf("\t\t\tchoose(0-6):"); } int main() { int n; menu(); scanf("%d",&n); while(n) { switch(n) { case 1: input(); break; case 2: search(); break; case 3: update(); break; case 4: del(); break; case 5: sort(); break; case 6: display(); break; default:break; } getch(); menu(); scanf("%d",&n); } } void input() { int i,count=0;//count用来记录通讯录中联系人的个数 char ch[2];//用来记录用户的输入 FILE *fp; if((fp=fopen("data.txt","a+"))==NULL)//创建一个文件,如果文件打开失败则返回空值 { printf("can not open\n"); return; } while(!feof(fp))//判断文件是否结束,如果没有调用fread函数统计记录条数 { if(fread(&addrinfo[count],LEN,1,fp)==1)//LEN统计结构体类型addritem中变量所有成员的长度,即所占的字节数 count++; } fclose(fp); if(count==0) printf("No contact record!\n"); else { system("cls"); display(); } if((fp=fopen("data.txt","wb"))==NULL) { printf("can not open!\n"); return; } for(i=0;i0) { t=addrinfo[i]; addrinfo[i]=addrinfo[j]; addrinfo[j]=t; } if((fp=fopen("data.txt","w"))==NULL) { printf("can not open\n"); return; } for(i=0;i