Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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); 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
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