#include <string.h>
#include <stdio.h>
#include <stdlib.h> //Needed for NULL, exit() and other things!!! 

//here we test the struct datatype

struct people {
  char name[50];
  char surname[50];
  char address[50];
  int age;
};

//vi har nu två 'person' variabler i 'people'

  void print_person (struct people person){
    printf("First name: %s\n",person.name);
    printf("Surname: %s\n",person.surname);
    printf("Address: %s\n",person.address);
    printf("Age: %d\n",person.age);
  }

int main (){
  struct people person1, person2;

  strcpy(person1.name, "John");
  strcpy(person1.surname, "Croft");
  strcpy(person1.address, "123 Main St.");
  person1.age = 51;  
  
  strcpy(person2.name, "Bengt");
  strcpy(person2.surname, "Brylde");
  strcpy(person2.address, "bladibla");
  person2.age = 59;  
  
  //print_person(person1);
  //print_person(person2);


  FILE *fp;
  fp = fopen("personfile.dat","w");
  if (fp != NULL){
    fwrite(&person1,sizeof(struct people),1,fp);
    fwrite(&person2,sizeof(struct people),1,fp);
    fclose(fp);
  }else{
    fprintf(stderr, "Problem opening file\n");
    exit(1);
  }
}


