Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

language C Write the function my_readline ) according to the following specifica

ID: 3719315 • Letter: L

Question

language C

Write the function my_readline ) according to the following specification Requires: * If *bufp is not NULL, then *bufp must point to a buffer that wa:s * previously allocated by malloc) or realloc() and *lenp must be less * than or equal to the size of that buffer. k Effects: *Attempts to read a ' '-terminated line from the given file descriptor, *fd. Copies this line, including the ' ', into the buffer referenced * by *bufp. If *bufp is NULL or the current size of the buffer, as given * by *lenp, is too small to store the entire line, then realloc) is * performed on the buffer to enlarge it. The enlarged buffer is returned *by reference uslng bufp. Similarly, the number of characters stored in * the buffer is returned by reference using lenp *Returns 0 upon success. Returns EOF if read() returns an end-of-file * indication before a , ' is encountered. Nonetheless, the buffer * referenced by *bufp will contain all of the characters successfully * read before that end-of-file indication, and *lenp will equal the number of characters in that buffer. Returns the value of errno if * either realloc) returns NULL or read() returns -1. In either case, as *with read() returning an end-of-file indication, the buffer referenced * by *bufp will contain all of the characters successfully read before *the error occurred, and *lenp will equal the number of characters in that buffer. Returns EINVAL if either bufp or lenp is NULL Notes: * In contrast to rio_readlineb), the returned line should not be NUI terminated * The only functions that your implementation may call are read (2) and * realloc(3) int my_readline(int fd, void **bufp, size_t *lenp) FILL THIS IN. */

Explanation / Answer

Hello!

Am implementing the question data from a file called "myFile.txt".

Not sure what the queation means by the read funuction. Am assuming it means a method which is used to read form the file.

So am going to use the function "getLine" which takes the input of the input of a buffer, the size of the buffer and the file from which to be read. getLine function is easy to use because it already reads only line by line so it reads the complete line. Also if the buffer size is less than the size of the line, then it automatically reallocs the buffer to appropriate size.

PS: Sorry, I do not have any C compiler setup on my computer right now, so was unable to paste a output.

#include <stdio.h>
#include <stdlib.h>
#include <erno.h>

int
my_readline (int fd, void **burf, size_t *len_p)
{
  
// the file we want to read from
char filename[] = "myfile.txt";
  

// open the file for reading
FILE * file = fopen (filename, "r");
  

// make sure the file opened properly
if (NULL == file)
  
{
  
fprintf (stderr, "Cannot open file: %s ", filename);
  
return 1;
  
}
  

// set up the buffer to read the line into. Don't worry too much
// if some of the lines are longer than 80 characters - the buffer
// will be made bigger if need be.
size_t buffer_size = 80;
len_p=0;
  
char *buffer = malloc (buffer_size * sizeof (char));
  
if(buffer == NULL || len_p == NULL)
return EINVAL;

if (-1 != getline (&burf, &buffer_size, file))
  
{
  

len_p++;
  
}
else{
return EOF;
}
  
fflush (stdout);
  

// make sure we close the file and clean up the buffer when we're
// finished
fclose (file);
  
free (buffer);
  

return 0;

}

EINVAL is included in the erno.h header.

Ask me if u have any doubts.