Question: In C , implement a readline ( ) function using only read ( 2 ) , open ( 2 ) , close ( 2 )
In C implement a readline function using only read open close free and malloc
Two global variables may be used, READLINEREADSIZE 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 initmyreadline 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 openfiletxt ORDONLY;
while str myreadlinefd NULL
printfs
str;
freestr;
closefd;
Yes it's also working with stdin :
printfs myreadline;
return ;
int main
char str NULL;
int fd openfiletxt ORDONLY;
while str myreadlinefd NULL
printfs
str;
freestr;
closefd;
Yes it's also working with stdin
printfs myreadline;
return ;
The function myreadline 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 endoffile 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 endoffile occurs before any characters have been read, the string remains unchanged.
Number of characters read will be set by the global variable READLINEREADSIZE You are allowed to maximum two global variables one for your
"storage" and one for READLINEREADSIZE Be ready, we will change the value of READLINEREADSIZE.
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 initmyreadline 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 gfsanitizeaddress
Static variables are strictly FORBIDDEN
Authorized functions
malloc
free
read
open
close
Unauthorized functions
Iseek
fread
fopen
Any other functions or system calls
Multiline macros are forbidden
Include another c is forbidden
Macros with logic whileifvariables are forbidden
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
