Question: Complete the below function bool readLine(FILE* fp, char** line, int* array_size); that reads a line from a file given by the file pointer fp, stores

 Complete the below function bool readLine(FILE* fp, char** line, int* array_size);

Complete the below function bool readLine(FILE* fp, char** line, int* array_size); that reads a line from a file given by the file pointer fp, stores the line into the C string *line, and updates the array size array_size if the array size of *line has changed. Your implementation should meet the following requirements

  • If *line is NULL, your function should use malloc to allocate enough memory.
  • If *line is not NULL and *array_size is not large enough to store the line, you should use realloc to resize the C string.
  • *array_size should be updated to reflect the array size of the character array.
  • The C string *line must be null-terminated.

bool readLine(FILE* fp, char** line, int* array_size) { // temp buffer, assuming a line has at most 1024 characters char temp[1024]; if (fgets(temp, 1024, fp)) { // TODO: temp contains the line, your tasks are: // 1. Handle the dynamic memory allocation for *line, // 2. Update *array_size if necessary // 3. Copy values from temp to *line // Note: line is a pointer to a c-string, when you // allocate memory, think carefully which // variable you need to use // Write your code below

Complete the below function bool readLine(FILE* fp, char** line, int* array_size); that reads a line from a file given by the file pointer fp, stores the line into the C string *line, and updates the array size array_size if the array size of *line has changed. Your implementation should meet the following requirements: If *line is NULL , your function should use malloc to allocate enough memory. If *line is not NULL and *array_size is not large enough to store the line, you should use realloc to resize the C string. *array_size should be updated to reflect the array size of the character array. The C string *line must be null-terminated. . bool readLine(FILE* fp, char** line, int* array_size) { // temp buffer, assuming a line has at most 1024 characters char temp [1024]; if (fgets (temp, 1024, fp)) { // TODO: temp contains the line, your tasks are: // 1. Handle the dynamic memory allocation for *line, // 2. Update *array_size if necessary // 3. Copy values from temp to *line // Note: line is a pointer to a c-string, when you // allocate memory, think carefully which // variable you need to use // Write your code below

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!