The C function get_next_line takes lines from a file or input stream and returns each line as a null-terminated string. Up until it meets a newline character or reaches the end of the file, it retrieves the following line.
Because the function is written in C, it requires the gcc compiler and a few common C libraries to run.
Simply include this header inside your file to access the function
#include "get_next_line.h"
Then include the source files and the appropriate flag when compiling your code:
get_next_line.c get_next_line_utils.c -D BUFFER_SIZE=<size>
To test your code you can use this following git repository:
#include "get_next_line.h"
#include <stdio.h>
#include <fcntl.h>
int main() {
int fd;
char *filename = "file.txt";
fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("Error opening file");
return 1;
}
char *line;
while ((line = get_next_line(fd)) != NULL) {
printf("%s\n", line);
free(line);
}
close(fd);
return 0;
}
gcc -Wall -Werror -Wextra -D BUFFER_SIZE=xx get_next_line.c get_next_line_utils.c && ./a.out