Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- FILE *fb,*fp;
- int position=0,n;
- int searcharr[100];
- typedef struct birth //defining birthday date
- {
- int day;
- int month;
- int year;
- }birth;
- birth readbirthday() //function for reading birthday from file (used in readcontact() function)
- {
- birth birthday;
- fscanf(fb,",%d",&birthday.day);
- fscanf(fb,"/%d",&birthday.month);
- fscanf(fb,"/%d",&birthday.year);
- return birthday;
- }
- typedef struct contact //defining contacts and its fields
- {
- char firstname[20];
- char lastname[20];
- birth birthday;
- char address[50];
- char city[20];
- int phonenumber;
- }contact;
- contact readcontact()
- {
- contact contacts;
- FILE * fb = fopen("book.txt","r");
- fseek(fb,position,0);
- fscanf(fb,"%[^,]",contacts.firstname);
- fscanf(fb,",%[^,]",contacts.lastname);
- fscanf(fb,",%d",&contacts.birthday.day);
- fscanf(fb,"/%d",&contacts.birthday.month);
- fscanf(fb,"/%d",&contacts.birthday.year);
- fscanf(fb,",%[^,]",contacts.address);
- fscanf(fb,",%[^,]",contacts.city);
- fscanf(fb,",%d\n",&contacts.phonenumber);
- position = ftell(fb);
- fclose(fb);
- return contacts;
- }
- void printcontact(contact contacts)
- {
- printf("\nFirst name is: %s\n",contacts.firstname);
- printf("Last name is: %s\n",contacts.lastname);
- printf("Birthday: %d/%d/%d\n",contacts.birthday.day,contacts.birthday.month,contacts.birthday.year);
- printf("Address: %s\n", contacts.address);
- printf("City: %s\n",contacts.city);
- printf("Phone number: 0%d\n",contacts.phonenumber);
- }
- void add_contact()
- {
- contact Contact;
- fp = fopen("book.txt","a");
- printf("Enter the contact's first name\t:");
- scanf("%s",Contact.firstname);
- fprintf(fp,"%s,",Contact.firstname);
- printf("Enter the contact's last name\t:");
- scanf("%s",Contact.lastname);
- fprintf(fp,"%s,",Contact.lastname);
- printf("Enter the birth date\n");
- printf("enter the day:\t");
- scanf("%d",&Contact.birthday.day);
- fprintf(fp,"%d/",Contact.birthday.day);
- printf("enter the month\t:");
- scanf("%d",&Contact.birthday.month);
- fprintf(fp,"%d/",Contact.birthday.month);
- printf("enter the year\t:");
- scanf("%d",&Contact.birthday.year);
- fprintf(fp,"%d,",Contact.birthday.year);
- printf("Enter the address: ");
- scanf(" %199[^\n]",Contact.address);
- fprintf(fp,"%s,",Contact.address);
- printf("Enter the city: ");
- scanf("%s",Contact.city);
- fprintf(fp,"%s,",Contact.city);
- printf("Enter phone number\t:");
- scanf("%d",&Contact.phonenumber);
- fprintf(fp,"%d\n",Contact.phonenumber);
- fclose(fp);
- }
- int count_contacts()
- {
- int count=0;
- fb = fopen("book.txt","r");
- while(!feof(fb))
- {
- char c;
- if((c=fgetc(fb))=='\n')
- count++;
- }
- fclose(fb);
- return count;
- }
- void display_menu()
- {
- int x;
- 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: ");
- scanf("%d",&x);
- switch(x)
- {
- case(1): add_contact();break;
- case(2): printf("add");
- case(3): printf("add");
- case(4): printf("add");
- case(5): ;break;
- case(7): printf("add");break;
- case(8): printf("add");break;
- case(9): printf("add");break;
- }
- }
- int substring(char name[],char key[])
- {
- int i,j,flag;
- for(i=0;i<=strlen(name)-strlen(key);i++)
- {
- for(j=0;j<strlen(key);j++)
- {
- flag = 1;
- if(key[j] != name[j+i])
- {
- flag = 0;
- break;
- }
- }
- if (flag ==1)
- break;
- }
- return flag;
- }
- /*int* searchcontact(char name[],char key[])
- {
- int i,j=0,n;
- int* arr = (int*) malloc(10*sizeof(int));
- printf("Enter the search key");
- scanf("%s",key);
- printf("%s",key);
- for(i=0;i<count;i++)
- {
- if(substring(contacts[i].firstname,key))
- {
- printf("contact found");
- arr[j]=i;
- j++;
- n=j;
- }
- }
- if(j==0)
- {
- printf("contact not found");
- return 0;
- }
- return arr;
- }*/
- int main()
- {
- // display_menu();
- int i,j=0;
- char key[20];
- // add_contact();
- int count = count_contacts();
- contact contacts[count];
- for(i=0;i<count;i++)
- contacts[i] = readcontact();
- // int* arr = (int*) malloc(count*sizeof(int));
- printf("Enter the search key: ");
- scanf("%s",key);
- // printf("%s",key);
- for(i=0;i<count;i++)
- {
- if(substring(contacts[i].firstname,key))
- {
- // printf("contact found");
- searcharr[j]=i;
- j++;
- n=j;
- }
- }
- if(j==0)
- printf("Search not found");
- if(j!=0)
- for(j=0;j<n;j++)
- printcontact(contacts[searcharr[j]]);
- /*for(i=0;i<count;i++)
- {
- printf("\nContact number %d:\n",i+1);
- printcontact(contacts[i]);
- }*/
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement