A line-by-line file reading function for C using static variables and buffer management.
get_next_line is a 42 School project that implements a function to read a file or input stream one line at a time. It must handle multiple file descriptors, manage buffers efficiently, and return each line including newline characters where appropriate.
The challenge is to build this functionality without using standard library functions like fgets()
or getline()
, focusing instead on file descriptors, memory handling, and string operations.
char *get_next_line(int fd);
- Reads a file or standard input line by line
- Supports multiple file descriptors simultaneously
- Efficient buffer management using
read()
- Custom string manipulation functions allowed only from libft
To compile your project with get_next_line.c
, include:
gcc -Wall -Wextra -Werror get_next_line.c get_next_line_utils.c -D BUFFER_SIZE=42
You can change BUFFER_SIZE
to test how the function behaves with different buffer lengths.
int fd = open("file.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)))
{
printf("%s", line);
free(line);
}
close(fd);
- 🔁 Loop-based reading using
read()
- 🧵 Static variables for persistent state between function calls
- 🧠 Manual memory management (
malloc
,free
) - 📂 File I/O using file descriptors
- ✂️ Custom string manipulation (
ft_strjoin
,ft_strdup
, etc.) - ⚙️ Edge case handling (EOF, empty lines, NULL return)
- 🔀 Managing multiple open files at once