#include <stdio.h>
#include <stdlib.h>

typedef struct { 
    int id; 
    char pname[20]; 
    char psurname[20]; 
    double salary;
} Person; 

Person Citizens[3];
Person *Omada;

void ShowData(Person *Atoma, int n){
  int i;
  for(i = 0; i < n; i++){
     printf ("%d) id = %d name = %s \t surname = %s \tsalary = %lf\n", i, 
	    Atoma[i].id, Atoma[i].pname, Atoma[i].psurname, Atoma[i].salary); 
   }
}

int main(int argc, char *argv[]) {
	printf("Size of Person : %d\n", sizeof(Person));
	printf("Size of Citizens : %d\n", sizeof(Citizens));
	printf("Size of Omada : %d\n", sizeof(Omada));

	ReadData();
	ReadDataDynamic();
	return 0;
}


void ReadData(){
	FILE *fp = fopen ("citizens.dat", "rb"); 
    if (fp == NULL)  { 
        printf("\nError opening file citizens.dat\n"); 
        exit (1); 
    } 

    if(fread( Citizens, sizeof(Citizens), 1, fp) != 1) {
       printf("\nAn error has occurred.\n");	
   }
   else {
   	ShowData(Citizens, 3);
   }
   fclose(fp);
}

void ReadDataDynamic(){
	FILE *fp = fopen ("citizens.dat", "rb"); 
    if (fp == NULL)  { 
        printf("\nError opening file citizens.dat\n"); 
        exit (1); 
    } 

    if( (Omada = (Person *) malloc(3 * sizeof(Person))) == NULL)  {
    	printf("Not enough memory.\n");
    	exit(1);
	}
    if(fread( Omada, sizeof(Person), 3, fp) != 3) {
       printf("\nAn error has occurred.\n");	
   }
   else {
   	ShowData(Omada, 3);
   }
   fclose(fp);
   free(Omada);
}



