My implementation of get_next_line for 42 School. Reads a file descriptor line by line, supports multiple file descriptors (bonus), and manages internal buffers efficiently. Includes all bonus functionality.
get_next_line is a function that reads one line at a time from a file descriptor.
It is used to process files or input streams efficiently and preserves the remainder between calls.
This version includes bonus functionality, allowing simultaneous reading from multiple file descriptors.
get_next_line.c— main line-reading functionget_next_line_utils*.c— helper functions for string manipulationget_next_line_bonus.c— extended functionality for multiple FDsget_next_line_bonus.h— bonus header definitions
Compile your program like this:
gcc main.c get_next_line.c get_next_line_utils.c -D BUFFER_SIZE=42Or for bonus:
gcc main.c get_next_line_bonus.c get_next_line_utils_bonus.c -D BUFFER_SIZE=42
You can alter BUFFER_SIZE to your liking or compile without it being that it has a default size.
🧪 Example Usage
#include "get_next_line.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int fd = open("text.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)))
{
printf("%s", line);
free(line);
}
close(fd);
return 0;
}
Remember to free each line after use to prevent memory leaks.
🧠 Key Concepts
-
Persistent static buffers
-
Reading with read() only once per buffer size
-
Handling multiple file descriptors independently (bonus)
-
Manual string manipulation (no strtok or external libraries)
⭐ Bonus Features
-
Multiple file descriptors
-
Independent buffer for each file
-
Correct handling of \n
-
No memory leaks
🧑💻 Author
Emanuel Tchipoque
🔗 LinkedIn: Emanuel Tchipoque