Listing 4

#include <stdio.h>

struct s_date
   {
   int day;
   int month;
   char filler;  /*  Just to get some packing bytes (perhaps) */
   int year;
   };

main()
   {
   FILE *file;
   static struct s_date date = {1,2,' ',3};
   static struct s_date date2 = {4,5,' ',6};

   printf("\n Size of the structure is %d",
      sizeof(struct s_date));

   /*  Write two structures out */
   file = fopen("DATA.DAT","w");
   fwrite(&date, sizeof(struct s_date), 1, file);
   fwrite(&date2, sizeof(struct s_date), 1, file);
   fclose(file);

   file = fopen("DATA.DAT","r");
   /*  Note reversal of which date is read */
   fread(&date2, sizeof(struct s_date), 1, file);
   fread(&date, sizeof(struct s_date), 1, file);
   fclose(file);
   }