I have a program in which I have a function already created. The function works how it was suppose to for the previous program, but now we are to improve it. So i need to re-implement this function and change it around to work a little differently, also i need to implement a safe malloc and safe realloc function, I have a safe malloc function, I don't know how to make a safe_realloc function though. Heres the code and what to do:
char *readLine(char *s, int n, FILE * fp, bool * overflow);
Although correct, this implementation has some shortcomings,
including the following:The user of this function must be able to
estimate a maximum line length.Characters are truncated if the maximum
line length is too small.The number of parameters to this function
makes it dicult to understand.(As a generalrule,
as the number of parameters increases,
it is often more dicult.
Imagine a function with twenty
parameters !)
1. Re - read the sections in our book which describe malloc()
and realloc().Implement \ safe "
versions of each of these. The idea is that if either memory allocation or reallocation fails wewant the program to stop. (See the solution
pmsort.c to see how to do this.)2. Re-implement readLine, using the following prototype:
char *readLine(FILE *fp);
Here's the result we want from the revised
readLine(). If the given le pointer is at theend of le, then readLine() should return NULL. Otherwise, it should allocate just the rightamount of memory|no more and no less|to store the next line of input. All characters up toand including the newline should be read and consumed; everything except the terminatingnewline should be stored in the allocated memory. As in the previous implementation, thestring of characters should be terminated with the null character.If there is not enough space available to store such a string, readline() should abort theprogram.For eciency, use the doubling method of allocation discussed in class. Also, for debuggingpurposes, your function should display the number of bytes allocated or reallocated for thecharacter string. This should be output immediately before the function is to return.
Demonstrate your function by using a modi ed version of readfile4.c.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define FILE_NAME "words" /* name of the input file */
#define MAX_WORD_LENGTH 8 /* adjust as needed */
char *readLine(char *s, int n, FILE * fp, bool * overflow);
int main(void)
{
FILE *fp; /* file pointer for an input stream */
int lineCount; /* number of lines processed */
bool lineOverflow; /* not enough room in char array? */
int nrErrors; /* how many lines overflow char array? */
char line[MAX_WORD_LENGTH + 1]; /* string to hold one line of input */
/* Try to open the input stream for reading */
fp = fopen(FILE_NAME, "r");
if (fp == NULL) {
fprintf(stderr, "Error: can't open %s for reading.\n", FILE_NAME);
exit(EXIT_FAILURE);
}
/* Process the file, line by line */
nrErrors = 0;
lineCount = 0;
while (readLine(line, sizeof(line), fp, &lineOverflow) != NULL) {
printf("Current line = \"%s\"", line);
lineCount++;
if (lineOverflow) {
nrErrors++;
printf(" (overflow in line: characters discarded.)\n");
} else
printf("\n");
}
printf("Lines processed = %d\n", lineCount);
printf("Number of lines which didn't fit = %d\n", nrErrors);
/* Close the stream */
fclose(fp);
return 0;
}
/* Read and store one line of input
return NULL if stream is at eof or there was a read error
otherwise returns the pointer s to the char array
reads and stores up to n characters in the char array, stopping at newline
if there isn't space to hold the entire line, overflow is set to true
and any trailing characters on that line are discarded
*/
char *readLine(char *s, int n, FILE * fp, bool * overflow)
{
int i; /* number of characters stored in the string */
int ch; /* most recent character read from the file */
int count; /* number of characters read */
i = 0;
count = 0;
/* Read characters on the current line, storing as many as will fit */
while (((ch = getc(fp)) != '\n') && (ch != EOF)) {
count++;
/* Safely deposit this character */
if (i < n - 1) /* hold one slot back for '\0' */
s[i++] = ch;
}
if ((count == 0) && (ch == EOF)) /* no characters read: at eof */
return NULL;
/* Stream was not at eof, so a line has been read */
s[i] = '\0'; /* properly terminate the string */
/* Too many characters on this line? */
if (count > i)
*overflow = true;
else
*overflow = false;
return s;
}
Also here is the safe_malloc function:
Code:
/* safely allocate a block of storage, aborting program if not possible */
void *safe_malloc(size_t size)
{
void *p;
p = malloc(size);
if (p == NULL) {
fprintf(stderr, "pmsort: error - out of space!");
exit(EXIT_FAILURE);
} else
return p;
}