Mega Code Archive

 
Categories / C / Data Structure Algorithm
 

Investigating the family

/* Beginning C, Third Edition  By Ivor Horton  ISBN: 1-59059-253-0  Published: Apr 2004  Publisher: apress */ #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> char myfile[] = "C:\\myfile.bin"; /* Physical file name      */ FILE *pfile = NULL;               /* File pointer            */ struct Date                       /* Structure for a date    */ {    int day;    int month;    int year; }; typedef struct family        /* Family structure declaration */ {    struct Date dob;    char name[20];    char pa_name[20];    char ma_name[20]; }Family; /* Function prototypes */ int get_person(Family *pfamily);       /* Input function           */ void show_person_data(void);           /* Output function          */ void get_parent_dob(Family *pfamily);  /* Function to find pa & ma */ void main() {    Family member;                      /* Stores a family structure */    Family *pmember = &member;     /* Points to the family structure */    if((pfile = fopen(myfile, "wb")) == NULL)    {      printf("\nUnable to open %s for writing.\n", myfile);      abort();    }    while(get_person(pmember))           /* As long as we have input */      fwrite(pmember, sizeof member, 1, pfile);  /*    write it away */    fclose(pfile);                 /* Close the file now its written */    show_person_data();            /* Show what we can find out      */    if(remove(myfile))      printf("\nUnable to delete %s.\n", myfile);    else      printf("\nDeleted %s OK.\n", myfile); } /* Function to input data on Family members */ int get_person( Family *temp) {    static char more = '\0';    /* Test value for ending input */    printf("\nDo you want to enter details of a%s person (Y or N)? ",                                        more != '\0'?"nother " : "" );    scanf(" %c", &more);    if(tolower(more) == 'n')            return 0;    printf("\nEnter the name of the person: ");    scanf("%s", temp->name);         /* Read the Family's name */    printf("\nEnter %s's date of birth (day month year); ", temp->name);    scanf("%d %d %d", &temp->dob.day, &temp->dob.month, &temp->dob.year);    printf("\nWho is %s's father? ", temp->name);    scanf("%s", temp->pa_name);      /* Get the father's name  */    printf("\nWho is %s's mother? ", temp->name);    scanf("%s", temp->ma_name);      /* Get the mother's name  */    return 1; } /* Function to output data on people on file   */ void show_person_data(void) {    Family member;             /* Structure to hold data from file  */    Family *pmember = &member; /* Pointer to Family structure       */    fpos_t current = 0;        /* File position                     */    pfile = fopen(myfile, "rb"); /* Open file for binary read  */    /* Read data on person */    while(fread(pmember, sizeof member, 1, pfile))       {      fgetpos(pfile, &current);  /* Save current position      */      printf("\n\n%s's father is %s, and mother is %s.",               pmember->name, pmember->pa_name, pmember->ma_name);      get_parent_dob(pmember);   /* Get parent data            */      fsetpos(pfile, &current);  /* Position file to read next */    }     fclose(pfile);              /* Close the file             */ } /* Function to find parents' dates of birth. */ void get_parent_dob(Family *pmember) {    Family testmem;             /* Stores a relative         */    Family *ptestmem = &testmem; /* Pointer to the relative   */    int num_found = 0;           /* Count of relatives found  */    rewind(pfile);               /* Set file to the beginning */    /* Get the stuff on a relative */    while(fread(ptestmem, sizeof(Family), 1, pfile))    {      if(strcmp(pmember->pa_name, ptestmem->name) == 0)   /*Is it pa? */      { /* We have found dear old dad */        printf("\n Pa was born on %d/%d/%d.",              ptestmem->dob.day, ptestmem->dob.month, ptestmem->dob.year);            if(++num_found == 2)    /* Increment parent count    */          return;               /* We got both so go home    */      }      else        if(strcmp(pmember->ma_name, ptestmem->name) == 0) /*Is it ma? */        { /* We have found dear old ma */          printf("\n Ma was born on %d/%d/%d.",                 ptestmem->dob.day, ptestmem->dob.month, ptestmem->dob.year);          if(++num_found == 2)  /* Increment parent count    */              return;           /* We got both so go home    */        }    } }