A C library that reads a single line from a file descriptor at a time, allocating memory for the line dynamically.
- Read lines from a file descriptor one line at a time.
- Stops reading at the first newline character ('\n') or the end of file (EOF).
- Dynamically alocate memory for the line, which must be freed by the caller.
- Handles multiple file descriptors simultaneously, without losing reading context (see bonus part).
- All functions have doxygen comments explaining their purpose in detail.
- Handles edge cases such as empty files, invalid file descriptors and memory allocaiton errors.
- Clone the repository.
% git clone https://github.com/RealConrad/42get-next-line.git
- Remember to include
get_next_line.h
and/orget_next-line_bonus.h
respectively in your c (source) files. - Compile with
get_next_line.c
andget_next_line_utils.c
and/orget_next_line_bonus.c
andget_next_line_utils_bonus.c
respectively.
An example on how to use the library:
File: main.c
#include "get_next_line.h"
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char **argv)
{
int fd;
char *line;
if (argc != 2)
{
printf("Usage: %s <filename>\n", argv[0]);
return (1);
}
fd = open(argv[1], O_RDONLY);
if (fd < 0)
{
printf("Error opening file");
return (1);
}
while ((line = get_next_line(fd)) != NULL)
{
printf("%s", line);
free(line);
}
close(fd);
return (0);
}
Compiling and executing:
% gcc -Wall -Wextra -Werror -D BUFFER_SIZE=32 main.c get_next_line.c get_next_line_utils.c -o gnl_test
% ./gnl_test <filename>.txt