Question
Finish the readPeople function and freePeople function in a C program given a file handle and an array of null pointers readPeople.c pasted below... /**********************************************************************************
Finish the readPeople function and freePeople function in a C program given a file handle and an array of null pointers
readPeople.c pasted below...
/********************************************************************************** * readPeople.c - Reads people (max 100), fills in array * * NOTES: * - Input is: * last * first * age * , no blank lines. * - Assume that input is valid (if you find a last name, then there are 2 more lines) * - Input is terminated w/a blank line or EOF * ********************************************************************************/
#include
#define CAP 100
typedef struct { char first[10] ; char last[20] ; int age ; } person ;
// reads people from file handle f, format described above // a must be large enough to hold all entries void readPeople( person* a[], FILE* f ) ;
// Returns heap memory pointed to by elements of a // a is of size n // Elements point to a person, or are NULL void freePeople( person* a[], size_t n ) ;
int main( int argc, char **argv ) { FILE *fin = stdin ;
person* team[CAP] = { NULL } ; /* rest are initialised to 0 */
if( argc>1 ) { fin = fopen( argv[1], "r" ) ; if( fin == NULL ) { fprintf( stderr, "Couldn't open %s for reading. Exiting. ", argv[1] ) ; exit( 1 ) ; } }
readPeople( team, fin ) ;
freePeople( team, CAP ) ;
return 0 ; }
void readPeople( person* a[], FILE* f ) { char *buff = NULL ; size_t len ; size_t cnt = 0 ;
while( getline( &buff, &len, f ) > 1 ) { // We read a last name (with the newline) // - get memory for person // - append to array // - read next 2 lines, fill it in
++cnt ; }
free( buff ) ; }
void freePeople( person* a[], size_t n ) { // Do NOT assume array is dense. Check every element }
2. Copy the file readPeople.c. The main function should be in fine shape. You need to finish the function readPeople, and supply the freePeople function. readPeople Given a file handle and an array of null pointers: . Read the information (last name, first name, age ) for a single person Store that information in a person instance allocated from the heap. o Append that instance to the array argument o Attempt to read another person Here is a sample input file: Smith Granny 68 Tape Scott 32 Wire Barbara 45 You may assume: Entries are complete. If there's a last name, then there's a first name and an age Data is correct. Age will always convert to an integer Q2 Place the readPeople function in your labsheet, with formatting. Consider Vim's :r (read) command, or use Bash redirect/append. freePeople This function simply returns the heap instances pointed to by the array to the heap. Do not assume that the array is dense. Check every position for a non-NULL pointer. Q3 Place the freePeople function in your labsheet, with formatting. Consider Vim's :r (read) command, or use Bash redirect/appendStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started