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;
- 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("First 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("\t\t\t\t\t\t* * * * * * * * * * *\n\t\t\t\t\t\t* P h o n e b o o k *\n\t\t\t\t\t\t* * * * * * * * * * *\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 main()
- {
- system("COLOR FC");
- display_menu();
- // int i;
- // add_contact();
- // int count = count_contacts();
- // contact contacts[count];
- // for(i=0;i<count;i++)
- // contacts[i] = readcontact();
- // 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