Write a program that reads data about people from a file.

Added on - 16 Sep 2019

  • 2

    pages

  • 429

    words

  • 83

    views

  • 0

    downloads

Showing pages 1 to 1 of 2 pages
Write a program that reads data about people from a file. You aresupposed to create a quick data file for testing purposes yourself,which has the following structure:each line contains a record of data about exactly one personeach record has the person's name, followed by a space,followed by the person's ageUnlike in other assignments, there is no indication of how manyrecords there will be in the file. Also, we don't want to assumethat you do a first scan to find out. Instead, I ask you to solve theproblem of creating enough space for all the records byincrementally reading one record at a time, put it into astructpersonand link that structure to the list of person structuresthat is already there. This means that your person structureneeds to contain a pointer to the next structure in the list. Thisalso means that you need to keep track of the structure that isthe head of the list, as well as the structure that is at the end ofthe list by maintaining corresponding pointers.Here is a critical piece of code. It reads a record from the file,creates the space (i.e. structure) to hold the data and updatesthe pointers involved:while(2 ==fscanf(people_data,"%s %d",person_current->name, &person_current->age)) {person_current->next=NULL;// provide space for the new last personperson_last->next= (structperson*)malloc(sizeof(structperson));// set the new last personperson_last = person_last->next;// copy the current's person data just readinto the new last person*person_last = *person_current;// update age total and number of recordsage_total += person_current->age;