Question: In C , implement a readline ( ) function using only read ( 2 ) , open ( 2 ) , close ( 2 )

In C, implement a readline() function using only read(2), open(2), close(2), free(3), and malloc(3).
Two global variables may be used, READLINE_READ_SIZE and another for a buffer.
The function should accept a file stream or stdin, and read each line until
or EOF. An additional function, void init_my_readline() should reinitialise the global variable and free allocated resources to allow for the next file to be read.
It should not use lseek, fread, or fopen.
Example usage:
int main ()
{
char *str = NULL;
int fd = open("./file.txt", O_RDONLY);
while ((str = my_readline(fd))!= NULL)
{
printf("%s
", str);
free(str);
}
close(fd);
//
// Yes it's also working with stdin :-)
// printf("%s", my_readline(0));
//
return 0;
}
int main ()
{
char *str = NULL;
int fd = open("./file.txt", O_RDONLY);
while ((str = my_readline(fd))!= NULL)
{
printf("%s
", str);
free(str);
}
close(fd);
//
// Yes it's also working with stdin
// printf("%s", my_readline(0));
//
return 0;
}
The function my_readline reads a line from the strean represented by fd and returns it into an allocated string (heap or stack ?). It stops when either the.
newline character is read or when the end-of-file is reached, whichever comes first. The newline character is not copied to the string.
On success, a pointer to the string is returned. On error, a null pointer is returned. If the end-of-file occurs before any characters have been read, the string remains unchanged.
Number of characters read will be set by the global variable READLINE_READ_SIZE You are allowed to maximum two global variables (one for your
"storage" and one for READLINE_READ_SIZE). Be ready, we will change the value of READLINE_READ_SIZE.
Requirements
Your code must be compiled with the flags -Wall -Wextra -Werror.
Technical description
In order to start reading another file you will also produce a function: void init_my_readline() which will init (or reinitialize) your global variable.
In the case of reinitialize, you will, obviously, free allocated resources.
Hints
Your function should work with any of streams, whether it be a file, standard input or a redirection...
Your function should be able to be used in a loop to read an entire file (see example)
Watch out for memory leaks !
You can test your code against memory errors by compiling with the debugging flags -g3-fsanitize=address
Static variables are strictly FORBIDDEN
Authorized function(s)
malloc(3)
free
read
open
close
Unauthorized function(s)
Iseek
fread
fopen
Any other functions or system calls
Multiline macros are forbidden
Include another .c is forbidden
Macros with logic (while/if/variables/...) are forbidden
In C , implement a readline ( ) function using

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 Programming Questions!