#include
#include
#include
FILE *fp;
int i;
char ch;
/*Address Struct Definition*/
struct member {
int number;
char name[20];
char telephone[13];
char address[50];
char post[6];
}members[50];
void Data_Menu(){
printf("\n-----------------------------------------------------------\n");
printf(" 1.Add");
printf(" 2.Delete");
printf(" 3.Search");
printf(" 4.Save");
printf(" 5.Exit");
printf("\神行n-----------------------------------------------------------\n");
}
void Data_Load(){
int j;
long k;
fp=fopen("members.txt","rt");
if(fp!=NULL){
for(i=1;i<50;i++){
j=fgetc(fp);
if(j==EOF){
return;
}
k=i-1;
fseek(fp,k*sizeof(struct member),SEEK_SET);
fread(&members[i],sizeof(struct member),1,fp);
}
}else{
fp=fopen("members.txt","w");
i=1;
}
}
void Data_Add(int j){
printf("\n");
members[j].number=j;
printf("\n\tNo:%d",j);
printf("\n\tName:");
scanf("%s",members[j].name);
printf("\tTelephone:");
scanf("%s",members[j].telephone);
printf("\tAddress:");
scanf("%s",members[j].address);
printf("\tPost:");
scanf("%s",members[j].post);
}
void Data_Show(int j){
printf("\n\tNo:%d",members[j].number);
printf("大瞎告\n\tName:%s",members[j].name);
printf("\n\tTelephone:%s",members[j].telephone);
printf("\n\tAddress:%s",members[j].address);
printf("\n\tPost:%s",members[j].post);
}
void Data_Save(){
int j;
fp=fopen("members.txt","w");
for(j=1;j<=i;j++){
fwrite(&members[j],sizeof(struct member),1,fp);
}
}
main(){
char *search_name;
int j,k;
Data_Load();
i--;
do{
Data_Menu();
printf(" Please Choose:");
ch=getche();
switch(ch){
case '1':
while(i<50){
i++;
Data_Add(i);
break;
}
if(i==50){
printf("\n\滚明tMessage is Filled!\n");
}
break;
case '2':
if(i<1){
printf("\n\tNo Records\n");
break;
}
printf("\n\tDelete No:");
scanf("%d",&k);
for(j=k;j strcpy(members[j].name,members[j+1].name);
strcpy(members[j].telephone,members[j+1].telephone);
strcpy(members[j].address,members[j+1].address);
strcpy(members[j].post,members[j+1].post);
}
i--;
break;
case '3':
printf("\n\tPlease Input Search Name:");
search_name = (char *)malloc(20);
gets(search_name);
for(j=1;j<=i;j++){
if(strcmp(search_name,members[j].name)==0){
Data_Show(j);
break;
}
}
free(search_name);
break;
case '4':
Data_Save();
printf(" Date is Saved!\n");
fclose(fp);
break;
case '5':
printf("\n\t");
printf(" Do You Want To Save Message(y/n)");
if(getche()=='y'){
Data_Save();
}
fclose(fp);
exit(0);
break;
default:
break;
}
}while(1);
}