Advertisement
yoyo106

functioning phonebook

Dec 16th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.59 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<windows.h>
  6. struct person
  7. {
  8.     char name[35];
  9.     char address[50];
  10.      char father_name[35];
  11.      char mother_name[30];
  12.     long int mble_no;
  13.     char sex[8];
  14.     char mail[100];
  15.     char citision_no[20];
  16.  
  17.     };
  18. void menu();
  19. void got();
  20. void start();
  21. void back();
  22. void addrecord();
  23. void listrecord();
  24. void modifyrecord();
  25. void deleterecord();
  26. void searchrecord();
  27. int main()
  28. {
  29.     system("color 5f");
  30.     start();
  31.     return 0;
  32. }
  33. void back()
  34. {
  35.     start();
  36. }
  37. void start()
  38. {
  39.     menu();
  40. }
  41. void menu()
  42. {
  43.     system("cls");
  44. printf("\t\t**********WELCOME TO PHONEBOOK*************");
  45.  
  46. printf("\n\n\t\t\t  MENU\t\t\n\n");
  47. printf("\t1.Add New   \t2.List   \t3.Exit  \n\t4.Modify \t5.Search\t6.Delete");
  48.  
  49. switch(getch())
  50. {
  51.     case '1':
  52.  
  53.                 addrecord();
  54.     break;
  55.    case '2': listrecord();
  56.     break;
  57.     case '3': exit(0);
  58.     break;
  59.     case '4': modifyrecord();
  60.     break;
  61.     case '5': searchrecord();
  62.     break;
  63.     case '6': deleterecord();
  64.     break;
  65.     default:
  66.                 system("cls");
  67.                 printf("\nEnter 1 to 6 only");
  68.                 printf("\n Enter any key");
  69.                 getch();
  70.  
  71. menu();
  72. }
  73. }
  74.         void addrecord()
  75. {
  76.         system("cls");
  77.         FILE *f;
  78.         struct person p;
  79.         f=fopen("project","ab+");
  80.         printf("\n Enter name: ");
  81.         got(p.name);
  82.         printf("\nEnter the address: ");
  83.         got(p.address);
  84.         printf("\nEnter father name: ");
  85.         got(p.father_name);
  86.         printf("\nEnter mother name: ");
  87.         got(p.mother_name);
  88.         printf("\nEnter phone no.:");
  89.         scanf("%ld",&p.mble_no);
  90.         printf("Enter sex:");
  91.         got(p.sex);
  92.         printf("\nEnter e-mail:");
  93.          got(p.mail);
  94.         printf("\nEnter citizen no:");
  95.         got(p.citision_no);
  96.         fwrite(&p,sizeof(p),1,f);
  97.  
  98.       fflush(stdin);
  99.         printf("\nrecord saved");
  100.  
  101. fclose(f);
  102.  
  103.     printf("\n\nEnter any key");
  104.  
  105.      getch();
  106.     system("cls");
  107.     menu();
  108. }
  109. void listrecord()
  110. {
  111.     struct person p;
  112.     FILE *f;
  113. f=fopen("project","rb");
  114. if(f==NULL)
  115. {
  116. printf("\nfile opening error in listing :");
  117.  
  118. exit(1);
  119. }
  120. while(fread(&p,sizeof(p),1,f)==1)
  121. {
  122.      printf("\n\n\n YOUR RECORD IS\n\n ");
  123.         printf("\nName=%s\nAdress=%s\nFather name=%s\nMother name=%s\nMobile no=%ld\nSex=%s\nE-mail=%s\nCitizen no=%s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.citision_no);
  124.  
  125.      getch();
  126.      system("cls");
  127. }
  128. fclose(f);
  129.  printf("\n Enter any key");
  130.  getch();
  131.     system("cls");
  132. menu();
  133. }
  134. void searchrecord()
  135. {
  136.     struct person p;
  137. FILE *f;
  138. char name[100];
  139.  
  140. f=fopen("project","rb");
  141. if(f==NULL)
  142. {
  143.     printf("\n error in opening\a\a\a\a");
  144.     exit(1);
  145.  
  146. }
  147. printf("\nEnter name of person to search\n");
  148. got(name);
  149. while(fread(&p,sizeof(p),1,f)==1)
  150. {
  151.     if(strcmp(p.name,name)==0)
  152.     {
  153.         printf("\n\tDetail Information About %s",name);
  154.         printf("\nName:%s\naddress:%s\nFather name:%s\nMother name:%s\nMobile no:%ld\nsex:%s\nE-mail:%s\nCitision no:%s",p.name,p.address,p.father_name,p.mother_name,p.mble_no,p.sex,p.mail,p.citision_no);
  155.     }
  156.         else
  157.         printf("file not found");
  158.  
  159. }
  160.  fclose(f);
  161.   printf("\n Enter any key");
  162.  
  163.      getch();
  164.     system("cls");
  165. menu();
  166. }
  167. void deleterecord()
  168. {
  169.     struct person p;
  170.     FILE *f,*ft;
  171.     int flag;
  172.     char name[100];
  173.     f=fopen("project","rb");
  174.     if(f==NULL)
  175.         {
  176.  
  177.             printf("CONTACT'S DATA NOT ADDED YET.");
  178.  
  179.         }
  180.     else
  181.     {
  182.         ft=fopen("temp","wb+");
  183.         if(ft==NULL)
  184.  
  185.             printf("file opaning error");
  186.         else
  187.  
  188.         {
  189.  
  190.  
  191.         printf("Enter CONTACT'S NAME:");
  192.         got(name);
  193.  
  194.         fflush(stdin);
  195.         while(fread(&p,sizeof(p),1,f)==1)
  196.         {
  197.             if(strcmp(p.name,name)!=0)
  198.                 fwrite(&p,sizeof(p),1,ft);
  199.             if(strcmp(p.name,name)==0)
  200.                 flag=1;
  201.         }
  202.     fclose(f);
  203.     fclose(ft);
  204.     if(flag!=1)
  205.     {
  206.         printf("NO CONACT'S RECORD TO DELETE.");
  207.         remove("temp.txt");
  208.     }
  209.         else
  210.         {
  211.             remove("project");
  212.             rename("temp.txt","project");
  213.             printf("RECORD DELETED SUCCESSFULLY.");
  214.  
  215.         }
  216.     }
  217. }
  218.  printf("\n Enter any key");
  219.  
  220.      getch();
  221.     system("cls");
  222. menu();
  223. }
  224.  
  225. void modifyrecord()
  226. {
  227.     int c;
  228.     FILE *f;
  229.     int flag=0;
  230.     struct person p,s;
  231.     char  name[50];
  232.     f=fopen("project","rb+");
  233.     if(f==NULL)
  234.         {
  235.  
  236.             printf("CONTACT'S DATA NOT ADDED YET.");
  237.             exit(1);
  238.  
  239.  
  240.         }
  241.     else
  242.     {
  243.         system("cls");
  244.         printf("\nEnter CONTACT'S NAME TO MODIFY:\n");
  245.             got(name);
  246.             while(fread(&p,sizeof(p),1,f)==1)
  247.             {
  248.                 if(strcmp(name,p.name)==0)
  249.                 {
  250.  
  251.  
  252.  
  253.                     printf("\n Enter name:");
  254.                     got(s.name);
  255.                     printf("\nEnter the address:");
  256.                     got(s.address);
  257.                     printf("\nEnter father name:");
  258.                     got(s.father_name);
  259.                     printf("\nEnter mother name:");
  260.                     got(s.mother_name);
  261.                     printf("\nEnter phone no:");
  262.                     scanf("%ld",&s.mble_no);
  263.                     printf("\nEnter sex:");
  264.                     got(s.sex);
  265.                     printf("\nEnter e-mail:");
  266.                     got(s.mail);
  267.                     printf("\nEnter citizen no\n");
  268.                     got(s.citision_no);
  269.                     fseek(f,-sizeof(p),SEEK_CUR);
  270.                     fwrite(&s,sizeof(p),1,f);
  271.                     flag=1;
  272.                     break;
  273.  
  274.  
  275.  
  276.                 }
  277.                 fflush(stdin);
  278.  
  279.  
  280.             }
  281.             if(flag==1)
  282.             {
  283.                 printf("\n your data id modified");
  284.  
  285.             }
  286.             else
  287.             {
  288.                     printf(" \n data is not found");
  289.  
  290.             }
  291.             fclose(f);
  292.     }
  293.      printf("\n Enter any key");
  294.      getch();
  295.     system("cls");
  296.     menu();
  297.  
  298. }
  299. void got(char *name)
  300. {
  301.  
  302.    int i=0,j;
  303.     char c,ch;
  304.     do
  305.     {
  306.         c=getch();
  307.                 if(c!=8&&c!=13)
  308.                 {
  309.                     *(name+i)=c;
  310.                         putch(c);
  311.                         i++;
  312.                 }
  313.                 if(c==8)
  314.                 {
  315.                     if(i>0)
  316.                     {
  317.                         i--;
  318.                     }
  319.                    // printf("h");
  320.                     system("cls");
  321.                     for(j=0;j<i;j++)
  322.                     {
  323.                         ch=*(name+j);
  324.                         putch(ch);
  325.  
  326.                     }
  327.  
  328.                 }
  329.     }while(c!=13);
  330.       *(name+i)='\0';
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement