Advertisement
yoyo106

Main

Dec 18th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. FILE *fb,*fp;
  5. int position=0;
  6. typedef struct birth //defining birthday date
  7. {
  8.     int day;
  9.     int month;
  10.     int year;
  11. }birth;
  12. birth readbirthday() //function for reading birthday from file (used in readcontact() function)
  13. {
  14.     birth birthday;
  15.     fscanf(fb,",%d",&birthday.day);
  16.     fscanf(fb,"/%d",&birthday.month);
  17.     fscanf(fb,"/%d",&birthday.year);
  18.     return birthday;
  19. }
  20.  
  21. typedef struct contact //defining contacts and its fields
  22. {
  23.     char firstname[20];
  24.     char lastname[20];
  25.     birth birthday;
  26.     char address[50];
  27.     char city[20];
  28.     int phonenumber;
  29. }contact;
  30. contact readcontact()
  31. {
  32.     contact contacts;
  33.     FILE * fb = fopen("book.txt","r");
  34.     fseek(fb,position,0);
  35.     fscanf(fb,"%[^,]",contacts.firstname);
  36.     fscanf(fb,",%[^,]",contacts.lastname);
  37.     fscanf(fb,",%d",&contacts.birthday.day);
  38.     fscanf(fb,"/%d",&contacts.birthday.month);
  39.     fscanf(fb,"/%d",&contacts.birthday.year);
  40.     fscanf(fb,",%[^,]",contacts.address);
  41.     fscanf(fb,",%[^,]",contacts.city);
  42.     fscanf(fb,",%d\n",&contacts.phonenumber);
  43.     position = ftell(fb);
  44.     fclose(fb);
  45.     return contacts;
  46. }
  47. void printcontact(contact contacts)
  48. {
  49.     printf("First name is: %s\n",contacts.firstname);
  50.     printf("Last name is: %s\n",contacts.lastname);
  51.     printf("Birthday: %d/%d/%d\n",contacts.birthday.day,contacts.birthday.month,contacts.birthday.year);
  52.     printf("Address: %s\n", contacts.address);
  53.     printf("City: %s\n",contacts.city);
  54.     printf("Phone number: 0%d\n",contacts.phonenumber);
  55.  
  56. }
  57. void add_contact()
  58. {
  59.     contact Contact;
  60.     fp = fopen("book.txt","a");
  61.     printf("Enter the contact's first name\t:");
  62.     scanf("%s",Contact.firstname);
  63.     fprintf(fp,"%s,",Contact.firstname);
  64.     printf("Enter the contact's last name\t:");
  65.     scanf("%s",Contact.lastname);
  66.     fprintf(fp,"%s,",Contact.lastname);
  67.     printf("Enter the birth date\n");
  68.     printf("enter the day:\t");
  69.     scanf("%d",&Contact.birthday.day);
  70.     fprintf(fp,"%d/",Contact.birthday.day);
  71.     printf("enter the month\t:");
  72.     scanf("%d",&Contact.birthday.month);
  73.     fprintf(fp,"%d/",Contact.birthday.month);
  74.     printf("enter the year\t:");
  75.     scanf("%d",&Contact.birthday.year);
  76.     fprintf(fp,"%d,",Contact.birthday.year);
  77.     printf("Enter the address: ");
  78.     scanf(" %199[^\n]",Contact.address);
  79.     fprintf(fp,"%s,",Contact.address);
  80.     printf("Enter the city: ");
  81.     scanf("%s",Contact.city);
  82.     fprintf(fp,"%s,",Contact.city);
  83.     printf("Enter phone number\t:");
  84.     scanf("%d",&Contact.phonenumber);
  85.     fprintf(fp,"%d\n",Contact.phonenumber);
  86.  
  87.     fclose(fp);
  88. }
  89. int count_contacts()
  90. {
  91.     int count=0;
  92.     fb = fopen("book.txt","r");
  93.     while(!feof(fb))
  94.     {
  95.         char c;
  96.         if((c=fgetc(fb))=='\n')
  97.             count++;
  98.     }
  99.     fclose(fb);
  100.     return count;
  101. }
  102. void display_menu()
  103. {
  104.     int x;
  105.     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: ");
  106.     scanf("%d",&x);
  107.     switch(x)
  108.     {
  109.         case(1): add_contact();break;
  110.         case(2): printf("add");
  111.         case(3): printf("add");
  112.         case(4): printf("add");
  113.         case(5): ;break;
  114.         case(7): printf("add");break;
  115.         case(8): printf("add");break;
  116.         case(9): printf("add");break;
  117.  
  118.     }
  119. }
  120. int main()
  121. {
  122.     system("COLOR FC");
  123.     display_menu();
  124. //    int i;
  125. //    add_contact();
  126. //    int count = count_contacts();
  127. //    contact contacts[count];
  128. //    for(i=0;i<count;i++)
  129. //    contacts[i] = readcontact();
  130. //    for(i=0;i<count;i++)
  131. //    {
  132. //        printf("\nContact number %d:\n",i+1);
  133. //        printcontact(contacts[i]);
  134. //
  135. //    }
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement