This function reads one line at a time from a given file descriptor, writes this line
into a dynamically allocated memory area and returns a pointer to this area.
This implementation enables to read the data from multiple files simultaniously, in any order, while “remembering” current position in files.
- The line is a series of characters, which end with a
'\n'
- The line returned is null-terminated (there is no new-line character).
- By calling the function in a loop, it will read the contents of a file given as an argument or stdin untill the EoF (ctrl + D) signal is sent.
to prevent memory leaks, the user must free(line)
after each call to get_next_line()
- The function returns 1 if a line has been read
- The function returns 0 if a file has been read (EoF)
- The function returns -1 on error
int get_next_line(const int fd, char **line);
where
- fd - file descriptor of a desired file
- ** line - pointer to the memory area where the contents of a line will be stored
To test the code, run sh compile.sh
.
This will make, link the library and will produce test_gnl executable file.
to run use ./gnl_test *read*
or ./gnl_test
to read from stdin
To use the function in the program, call it according to its prototype.