get_next_line is a project from the École 42 common core.
The goal is to implement a function that reads and returns one line at a time from a file descriptor, handling memory, buffers, and edge cases like a pro. 🧠
- Master file I/O in C (
read,open,close) - Handle buffers, memory allocation, and string manipulation
- Build a function that returns a line with each call, including:
- Lines with/without
\n - Empty lines
- Very long lines
- Lines with/without
- Manage multiple file descriptors at once
char *get_next_line(int fd);Reads from the file descriptor fd and returns the next line each time it’s called.
Returns NULL when there’s nothing left to read or an error occurs.
get_next_line/
├── get_next_line.c # Core logic
├── get_next_line_utils.c # String manipulation helpers
├── get_next_line.h # Function prototypes and macros
├── main.c # Optional test file
├── Makefile # Build system
make#include "get_next_line.h"
int fd = open("file.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)) != NULL)
{
printf("%s", line);
free(line);
}
close(fd);You can define BUFFER_SIZE at compile time:
gcc -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c main.cOr inside get_next_line.h:
#ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
#endifman 2 readman 3 malloc- [42 Subject PDF / Peer feedback]
42 Login: hsilverb
Contact: LinkedIn
This project was developed as part of the 42 School curriculum.
For educational and personal use only.