Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 KB | None | 0 0
  1.  
  2.  
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>
  6.  
  7.  
  8.  
  9. struct Birthday{
  10.    
  11.     int date;
  12.     char month[10];
  13.     int year;
  14.  
  15. };
  16.  
  17. struct Address{
  18.    
  19.     char country[10];
  20.     char city[10];
  21.     char street[30];
  22.  
  23. };
  24.  
  25. struct Person_Information{
  26.    
  27.     struct Birthday birthday;
  28.     struct Address address;
  29.  
  30. }person1, person2, read_person1, read_person2;
  31.  
  32.  
  33. int main()
  34. {
  35.     typedef struct Birthday Birthday;
  36.    
  37.    
  38.     FILE *fp;
  39.     if((fp = fopen("file.bin", "wb")) == NULL){
  40.         printf("Error");
  41.     }
  42.     person1.birthday.date = 12;
  43.     strcpy(person1.birthday.month, "September");
  44.     person1.birthday.year = 1999;
  45.     strcpy(person1.address.country, "Bulgaria");
  46.     strcpy(person1.address.city, "Sofia");
  47.     strcpy(person1.address.street, "Vladichina livada");
  48.     person2.birthday.date = 12;
  49.     strcpy(person2.birthday.month, "September");
  50.     person2.birthday.year = 1999;
  51.     strcpy(person2.address.country, "Bulgaria");
  52.     strcpy(person2.address.city, "Plovdiv");
  53.     strcpy(person2.address.street, "Hristo Botev");
  54.  
  55.     fwrite(&person1, 1, sizeof(Birthday), fp);
  56.     fwrite(&person2, 1, sizeof(Birthday), fp);
  57.     fclose(fp);
  58.  
  59.     if((fp = fopen("file.bin", "rb")) == NULL){
  60.         printf("Error");
  61.     }
  62.  
  63.     fread(&read_person1, 1, sizeof(Birthday), fp);
  64.     fread(&read_person2, 1, sizeof(Birthday), fp);
  65.     printf("Personal Information\n");
  66.     printf("Person 1:\n Birthday: %d %s %d\n Address: %s %s %s\n",read_person1.birthday.date, read_person1.birthday.month, read_person1.birthday.year, read_person1.address.country, read_person1.address.city,read_person1.address.street);
  67.     printf("Person 2:\n Birthday: %d %s %d\n Address: %s %s %s\n",read_person2.birthday.date, read_person2.birthday.month, read_person2.birthday.year, read_person2.address.country, read_person2.address.city,read_person2.address.street);
  68.     fclose(fp);
  69.    
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement