Telephone Directory Project
In this project “Telephone Directory System” we will maintain details like telephone number, name of customer, Address details like house number , name of street, name of city, name of state, name of country and pincode of the place.
In this project we have used text file handling to save the product records and perform various operations.
/* telephone directory project*/ #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> struct phone { char tno[15]; char fname[15],lname[15]; char add1[15],street[15],city[15],state[15],country[15],pincode[10]; }; void line() { int i; for(i=1;i<=95;printf("="),i++); printf("\n"); } void heading() { int i; system("color 0C"); //system("color 0A"); printf("\n\n\n\n"); printf("%55s","Telephone Directory System\n"); printf("%55s","=========================\n\n"); printf("%50s","Telephone Records\n"); printf("%50s","===============\n"); line(); printf("%10s %10s %10s %10s %10s %10s %10s %10s\n","Tel no","Name","hno", "Street","City","State","Country","Pincode"); line(); } void add_phone() { FILE *fp; struct phone p; fp=fopen("phone.dat","r"); if (fp==NULL) { printf("File is not present , it has to be created\n"); fp=fopen("phone.dat","w"); } else { printf("File is present , so open in append mode \n"); fclose(fp); fp=fopen("phone.dat","a"); } printf("\n\n\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the Telephone no "); scanf("%s",&p.tno); printf("%50s","Enter the First name "); scanf("%s",p.fname); strupr(p.fname); printf("%50s","Enter the Last name "); scanf("%s",p.lname); strupr(p.lname); printf("%50s","Enetr the house no "); scanf("%s",&p.add1); printf("%50s","Enter the Street "); scanf("%s",&p.street); printf("%50s","Enter the City "); scanf("%s",&p.city); printf("%50s","Enter the State "); scanf("%s",&p.state); printf("%50s","Enter the Country "); scanf("%s",&p.country); printf("%50s","Enter the Pincode"); scanf("%s",&p.pincode); fwrite(&p,sizeof(p),1,fp); fclose(fp); } void disp_all_phones() { FILE *fp; struct phone p; fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } line(); fclose(fp); } void phone_search_tno() { FILE *fp; struct phone p; int z; char tn[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the Telephone no "); scanf("%s",tn); fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tn,p.tno)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } } fclose(fp); if (z==0) printf("record is not present\n"); else line(); } void phone_search_fname() { FILE *fp; struct phone p; int z; char tfna[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the First Name "); scanf("%s",tfna); strupr(tfna); fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tfna,p.fname)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } } fclose(fp); if (z==0) printf("record is not present\n"); else line(); } void phone_search_lname() { FILE *fp; struct phone p; int z; char tlna[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the Last Name "); scanf("%s",tlna); strupr(tlna); fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tlna,p.lname)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } } fclose(fp); if (z==0) printf("record is not present\n"); else line(); } /* Search a record using street*/ void phone_search_street() { FILE *fp; struct phone p; int z; char tsna[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the Street Name "); scanf("%s",tsna); strupr(tsna); fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tsna,p.street)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } } fclose(fp); if (z==0) printf("record is not present\n"); else line(); } /* Search a record using city*/ void phone_search_city() { FILE *fp; struct phone p; int z; char tcna[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the City Name "); scanf("%s",tcna); strupr(tcna); fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tcna,p.city)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } } fclose(fp); if (z==0) printf("record is not present\n"); else line(); } /* Search a record using state*/ void phone_search_state() { FILE *fp; struct phone p; int z; char tsna[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the State Name "); scanf("%s",tsna); strupr(tsna); fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tsna,p.state)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } } fclose(fp); if (z==0) printf("record is not present\n"); else line(); } /* Search a record using Country*/ void phone_search_country() { FILE *fp; struct phone p; int z; char tcna[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the Country Name "); scanf("%s",tcna); strupr(tcna); fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tcna,p.country)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } } fclose(fp); if (z==0) printf("record is not present\n"); else line(); } /* Search a record using Pincode*/ void phone_search_pincode() { FILE *fp; struct phone p; int z; char tpin[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the PinCode "); scanf("%s",tpin); strupr(tpin); fp=fopen("phone.dat","r"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tpin,p.pincode)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } } fclose(fp); if (z==0) printf("record is not present\n"); else line(); } /* delete a record by telephone no */ void phone_delete_tno() { FILE *fp,*tfp; struct phone p; int z; char tno[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the Phone No "); scanf("%s",&tno); strupr(tno); fp=fopen("phone.dat","r"); tfp=fopen("temp","w"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tno,p.tno)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } else fwrite(&p,sizeof(p),1,tfp); } fclose(fp); fclose(tfp); if (z==0) printf("record is not present\n"); else { line(); remove("phone.dat"); rename("temp","phone.dat"); } } /* delete a record by First Name */ void phone_delete_fname() { FILE *fp,*tfp; struct phone p; int z; char tfna[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the First Name "); scanf("%s",&tfna); strupr(tfna); fp=fopen("phone.dat","r"); tfp=fopen("temp","w"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tfna,p.fname)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } else fwrite(&p,sizeof(p),1,tfp); } fclose(fp); fclose(tfp); if (z==0) printf("record is not present\n"); else { line(); remove("phone.dat"); rename("temp","phone.dat"); } } /* delete a record by Last Name */ void phone_delete_lname() { FILE *fp,*tfp; struct phone p; int z; char tlna[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the Last Name "); scanf("%s",&tlna); strupr(tlna); fp=fopen("phone.dat","r"); tfp=fopen("temp","w"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tlna,p.lname)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } else fwrite(&p,sizeof(p),1,tfp); } fclose(fp); fclose(tfp); if (z==0) printf("record is not present\n"); else { line(); remove("phone.dat"); rename("temp","phone.dat"); } } /* delete a record by PinCode */ void phone_delete_pincode() { FILE *fp,*tfp; struct phone p; int z; char tpin[15]; z=0; printf("\n\n"); printf("%55s","Telephone Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Enter the PinCode "); scanf("%s",&tpin); strupr(tpin); fp=fopen("phone.dat","r"); tfp=fopen("temp","w"); heading(); while((fread(&p,sizeof(p),1,fp))) { if (strcmpi(tpin,p.pincode)==0) { z=1; printf("%10s %10s %10s %10s %10s %10s %10s %10s\n",p.tno,p.fname, p.lname,p.add1,p.street,p.city,p.state,p.pincode); } else fwrite(&p,sizeof(p),1,tfp); } fclose(fp); fclose(tfp); if (z==0) printf("record is not present\n"); else { line(); remove("phone.dat"); rename("temp","phone.dat"); } } void menu() { system("cls"); system("color 0A"); printf("\n\n\n\n"); printf("%55s","Product Management System\n"); printf("%55s","=========================\n\n"); printf("%50s","Main Menu \n"); printf(" 1. Add a record\n"); printf(" 2. Display all the records \n"); printf(" 3. Display a record by phone no \n"); printf(" 4. Display a record by First Name \n"); printf(" 5. Display a record by Last Name \n"); printf(" 6. Display a record by Street Name \n"); printf(" 7. Display a record by City Name \n"); printf(" 8. Display a record by State Name \n"); printf(" 9. Display a record by Country Name \n"); printf(" 10. Display a record by PincCode \n"); printf(" 11. Delete a record by phone no\n"); printf(" 12. Delete a record by First Name \n"); printf(" 13. Delete a record by Last Name \n"); printf(" 14. Delete a record by PinCode \n"); //printf(" 13. to modify a record by prod number \n"); //printf(" 8. to modify a record by prod name \n"); printf(" 0. Exit \n"); printf(" Enter your choice "); } int main() { FILE *fp,*tfp; struct phone p; int op=100,n,trec,reclen=0,filelen=0,tn,z=0; char tna[10]; while(op!=0) { menu(); scanf("%d",&op); system("cls"); switch(op) { case 1: add_phone(); break; case 2: disp_all_phones(); break; case 3: phone_search_tno(); break; case 4: phone_search_fname(); break; case 5: phone_search_lname(); break; case 6: phone_search_street(); break; case 7: phone_search_city(); break; case 8: phone_search_state(); break; case 9: phone_search_country(); break; case 10: phone_search_pincode(); break; case 11: phone_delete_tno(); break; case 12: phone_delete_fname(); break; case 13: phone_delete_lname(); break; case 14: phone_delete_pincode(); break; case 0: printf("End of the program\n"); break; default: printf("Invalid option \n"); break; } getch(); } return(0); }