/* File: phone book.c Author: Brolly Description: Phone Book Date: 25/05/07 */ #include #include #include void euresh(struct list *infoz); void kataxwrhsh(struct list *infoz); void provolh(struct list *infoz); void del(struct list *infoz); void index(); struct list { char *name; char *phone; }; void main() { //FILE *fp; //int check=0; int choice=0; struct list* infoz; infoz=(struct list*)malloc(1*sizeof(struct list)); /*fp=fopen("tilefwna.dat","r"); if(fp==NULL) { check++; } else { fclose(fp); } if(check>0) { fp=fopen("tilefwna.dat","w"); fprintf(fp,"%s","--START--\n--EOF--"); fclose(fp); }*/ infoz[1].name=(char*)malloc(20*sizeof(char)); infoz[1].phone=(char*)malloc(20*sizeof(char)); printf("\t\t\t THLEFWNIKOS KATALOGOS\n\n"); printf("\t\t\t1.EURESH\n"); printf("\t\t\t2.KATAXWRHSH\n"); printf("\t\t\t3.DIAGRAFH\n"); printf("\t\t\t4.PROVOLH KATALOGOU\n"); printf("\t\t\t5.EKSODOS\n"); do { scanf("%d",&choice); if(choice==1) { system("cls"); printf("\n\t\tONOMA PROS EURESH : "); scanf("%s",infoz[1].name); printf("\n"); euresh(infoz); } if(choice==2) { system("cls"); printf("\n\t\tONOMA : "); scanf("%s",infoz[1].name); printf("\n\t\tTHLEFWNO : "); scanf("%s",infoz[1].phone); kataxwrhsh(infoz); } if(choice==3) { system("cls"); printf("\t\tNAME : "); scanf("%s",infoz[1].name); del(infoz); } if(choice==4) { system("cls"); printf("NAMES"); printf("\t\tPHONES\n\n"); provolh(infoz); } if(choice==5) exit(1); }while((choice!=1) && (choice!=2) && (choice!=3) && (choice!=4) && (choice!=5)); free(infoz[1].name); free(infoz[1].phone); } void euresh(struct list *infoz) { char *p; int count=0; int count2=0; FILE *file1; char *fBuffer=(char*)malloc(50*sizeof(char)); file1=fopen("tilefwna.dat","r"); while(feof(file1)==0) { fgets(fBuffer,50,file1); count++; count2++; if((p=strstr(fBuffer,infoz[1].name))!=0) { count--; printf("\t\t%s",p); } } printf("\n"); if(count==count2) { printf("\t\tDen yparxei to onoma.\n"); } printf("\n\n\n"); fclose(file1); free(fBuffer); index(); } void kataxwrhsh(struct list *infoz) { FILE *file; file=fopen("tilefwna.dat","a+"); fprintf(file,"\n%s",infoz[1].name); fprintf(file,"\t%s",infoz[1].phone); fclose(file); index(); } void provolh(struct list *infoz) { FILE *file; char fBuffer; file=fopen("tilefwna.dat","r"); while((fBuffer=fgetc(file))!=EOF) printf("%c",fBuffer); printf("\n\n"); fclose(file); index(); } void del(struct list *infoz) { FILE *file; FILE *file2; char *fBuffer=(char*)malloc(50*sizeof(char)); char *z; int counter=0; file=fopen("tilefwna.dat","r"); file2=fopen("temp","w"); while(feof(file)==0) { fgets(fBuffer,50,file); if((z=strstr(fBuffer,infoz[1].name))!=0) { counter++; printf("\n\t\tDIAGRAFIKE H KATAXWRHSH < %s >\n",infoz[1].name); continue; } else fprintf(file2,"%s",fBuffer); } if(counter==0) printf("\n\t\tTO ONOMA DEN YPARXEI."); printf("\n\n\n"); fclose(file); fclose(file2); free(fBuffer); remove("tilefwna.dat"); rename("temp","tilefwna.dat"); index(); } void index() { int a; a=getch(); if(a==13) { system("cls"); main(); } }