Advertisement
Guest User

new with search

a guest
Dec 18th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. FILE *fb,*fp;
  5. int position=0,n;
  6. int searcharr[100];
  7. typedef struct birth //defining birthday date
  8. {
  9.     int day;
  10.     int month;
  11.     int year;
  12. }birth;
  13. birth readbirthday() //function for reading birthday from file (used in readcontact() function)
  14. {
  15.     birth birthday;
  16.     fscanf(fb,",%d",&birthday.day);
  17.     fscanf(fb,"/%d",&birthday.month);
  18.     fscanf(fb,"/%d",&birthday.year);
  19.     return birthday;
  20. }
  21.  
  22. typedef struct contact //defining contacts and its fields
  23. {
  24.     char firstname[20];
  25.     char lastname[20];
  26.     birth birthday;
  27.     char address[50];
  28.     char city[20];
  29.     int phonenumber;
  30. }contact;
  31. contact readcontact()
  32. {
  33.     contact contacts;
  34.     FILE * fb = fopen("book.txt","r");
  35.     fseek(fb,position,0);
  36.     fscanf(fb,"%[^,]",contacts.firstname);
  37.     fscanf(fb,",%[^,]",contacts.lastname);
  38.     fscanf(fb,",%d",&contacts.birthday.day);
  39.     fscanf(fb,"/%d",&contacts.birthday.month);
  40.     fscanf(fb,"/%d",&contacts.birthday.year);
  41.     fscanf(fb,",%[^,]",contacts.address);
  42.     fscanf(fb,",%[^,]",contacts.city);
  43.     fscanf(fb,",%d\n",&contacts.phonenumber);
  44.     position = ftell(fb);
  45.     fclose(fb);
  46.     return contacts;
  47. }
  48. void printcontact(contact contacts)
  49. {
  50.     printf("\nFirst name is: %s\n",contacts.firstname);
  51.     printf("Last name is: %s\n",contacts.lastname);
  52.     printf("Birthday: %d/%d/%d\n",contacts.birthday.day,contacts.birthday.month,contacts.birthday.year);
  53.     printf("Address: %s\n", contacts.address);
  54.     printf("City: %s\n",contacts.city);
  55.     printf("Phone number: 0%d\n",contacts.phonenumber);
  56.  
  57. }
  58. void add_contact()
  59. {
  60.     contact Contact;
  61.     fp = fopen("book.txt","a");
  62.     printf("Enter the contact's first name\t:");
  63.     scanf("%s",Contact.firstname);
  64.     fprintf(fp,"%s,",Contact.firstname);
  65.     printf("Enter the contact's last name\t:");
  66.     scanf("%s",Contact.lastname);
  67.     fprintf(fp,"%s,",Contact.lastname);
  68.     printf("Enter the birth date\n");
  69.     printf("enter the day:\t");
  70.     scanf("%d",&Contact.birthday.day);
  71.     fprintf(fp,"%d/",Contact.birthday.day);
  72.     printf("enter the month\t:");
  73.     scanf("%d",&Contact.birthday.month);
  74.     fprintf(fp,"%d/",Contact.birthday.month);
  75.     printf("enter the year\t:");
  76.     scanf("%d",&Contact.birthday.year);
  77.     fprintf(fp,"%d,",Contact.birthday.year);
  78.     printf("Enter the address: ");
  79.     scanf(" %199[^\n]",Contact.address);
  80.     fprintf(fp,"%s,",Contact.address);
  81.     printf("Enter the city: ");
  82.     scanf("%s",Contact.city);
  83.     fprintf(fp,"%s,",Contact.city);
  84.     printf("Enter phone number\t:");
  85.     scanf("%d",&Contact.phonenumber);
  86.     fprintf(fp,"%d\n",Contact.phonenumber);
  87.  
  88.     fclose(fp);
  89. }
  90. int count_contacts()
  91. {
  92.     int count=0;
  93.     fb = fopen("book.txt","r");
  94.     while(!feof(fb))
  95.     {
  96.         char c;
  97.         if((c=fgetc(fb))=='\n')
  98.             count++;
  99.     }
  100.     fclose(fb);
  101.     return count;
  102. }
  103. void display_menu()
  104. {
  105.     int x;
  106.     printf("***********\n*Phonebook*\n***********\n\nWhat do you want?\n1-Add contact\n2-Delete\n3-Modify\n4-Search\n5-Print\n6-Save\n7-Quit\nPlease enter a number: ");
  107.     scanf("%d",&x);
  108.     switch(x)
  109.     {
  110.         case(1): add_contact();break;
  111.         case(2): printf("add");
  112.         case(3): printf("add");
  113.         case(4): printf("add");
  114.         case(5): ;break;
  115.         case(7): printf("add");break;
  116.         case(8): printf("add");break;
  117.         case(9): printf("add");break;
  118.  
  119.     }
  120. }
  121. int substring(char name[],char key[])
  122. {
  123.     int i,j,flag;
  124.     for(i=0;i<=strlen(name)-strlen(key);i++)
  125.     {
  126.  
  127.         for(j=0;j<strlen(key);j++)
  128.         {
  129.             flag = 1;
  130.         if(key[j] != name[j+i])
  131.         {
  132.             flag = 0;
  133.             break;
  134.         }
  135.  
  136.         }
  137.         if (flag ==1)
  138.             break;
  139.  
  140.  
  141.     }
  142.     return flag;
  143. }
  144. /*int* searchcontact(char name[],char key[])
  145. {
  146.     int i,j=0,n;
  147.     int* arr = (int*) malloc(10*sizeof(int));
  148.     printf("Enter the search key");
  149.     scanf("%s",key);
  150.     printf("%s",key);
  151.     for(i=0;i<count;i++)
  152.     {
  153.         if(substring(contacts[i].firstname,key))
  154.         {
  155.            printf("contact found");
  156.            arr[j]=i;
  157.            j++;
  158.            n=j;
  159.         }
  160.     }
  161.     if(j==0)
  162.     {
  163.         printf("contact not found");
  164.         return 0;
  165.     }
  166.     return arr;
  167.  
  168. }*/
  169. int main()
  170. {
  171. //    display_menu();
  172.     int i,j=0;
  173.     char key[20];
  174.  //   add_contact();
  175.     int count = count_contacts();
  176.     contact contacts[count];
  177.     for(i=0;i<count;i++)
  178.     contacts[i] = readcontact();
  179. //    int* arr = (int*) malloc(count*sizeof(int));
  180.  
  181.     printf("Enter the search key: ");
  182.     scanf("%s",key);
  183.    // printf("%s",key);
  184.     for(i=0;i<count;i++)
  185.     {
  186.         if(substring(contacts[i].firstname,key))
  187.            {
  188.               //  printf("contact found");
  189.                 searcharr[j]=i;
  190.                 j++;
  191.                 n=j;
  192.            }
  193.     }
  194.     if(j==0)
  195.         printf("Search not found");
  196.     if(j!=0)
  197.     for(j=0;j<n;j++)
  198.         printcontact(contacts[searcharr[j]]);
  199.  
  200.  
  201.     /*for(i=0;i<count;i++)
  202.     {
  203.         printf("\nContact number %d:\n",i+1);
  204.         printcontact(contacts[i]);
  205.  
  206.     }*/
  207.  
  208.     return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement