Filling a structure

This is a discussion on Filling a structure within the C Programming forums, part of the General Programming Boards category; Hi. I have to create a genealogy list reading a file with the following input: 10/10/1983 , Tom , Mark ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Filling a structure

    Hi. I have to create a genealogy list reading a file with the following input:

    10/10/1983,Tom,Mark,Julia
    ...
    ...
    11/02/1993,Tim,Sam,Jenny

    I have X strings in the file, every string contains a date, the person name, then the name of the father and the name of the mother.

    I'm creating a list of persons, reading the input file.

    Code:
    typedef struct {
      char date[MAX_PATH];
      char nameSurname[MAX_PATH];
      struct person * father;
      struct person * mother;
    } person;
    
    typedef struct list {
      person * aPerson;
      struct list * next;
    } peopleList;
    
    peopleList * head = NULL;
    
    void addPerson(person * thePerson){
      peopleList * tmp = (peopleList *)malloc(sizeof(peopleList));
      tmp->aPerson = thePerson;
      tmp->next = head;
      head = tmp;
    }
    
    void createPerson(char * aDate, char * aName, char * aFather, char * aMother){
      person * tmp;
      tmp = (person *)malloc(sizeof(person));
      strcpy(tmp->date, aDate);
      strcpy(tmp->nameSurname, aName);
      // HERE I SHOULD GET THE POINTERS TO THE FATHER AND MOTHER (IF ANY.. NULL OTHERWISE)
      addPerson(tmp);
    }
    The problem is that when I want to create a person (while tokenizing the input) before inserting it, I have to look in the previously created person list If I already know the mother and the father of the person I'm creating and I don't know how to get the pointer to them that I will put in the person structure I'm creating.
    Last edited by BianConiglio; 12-04-2005 at 04:44 PM.
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Well if you only need to look at the previous entry, then after you add and set your pointer to next, set another pointer to the current entry. Then no matter what entry you're on, you still have a pointer to the entry before it. If you have to look at all the entries then you have to set your pointer back to the root and transverse through the list again searching for whatever you need.
    Sent from my iPadŽ

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    this is a serach code to check wheather father and monther exsist in the list before. if so the pointer to that node return.

    Code:
    struct peoplelist * search(struct peoplelist *P, char father[], char mother [])
    {
        while(P != NULL)
        {
            if((strcmp(P->father,father)==0) && (strcmp(P->mother,mother)==0))
            return p;
            
            P=P->next;
        }
        return NULL;
    }
    ssharish2005
    Last edited by ssharish2005; 12-05-2005 at 05:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help filling a structure with data from a file.
    By System_159 in forum C++ Programming
    Replies: 41
    Last Post: 09-27-2006, 06:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Quick way of filling structure?
    By voodoo3182 in forum C Programming
    Replies: 3
    Last Post: 08-05-2005, 07:28 AM
  4. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:05 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21