42 Urduliz — Common Core project
A C function that reads a single line from a file descriptor each time it is called — like fgets but for any file descriptor (files, stdin, sockets).
char *get_next_line(int fd);Returns the next line (including \n) or NULL on EOF or error. Calling it in a loop reads the entire file line by line.
#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);Uses a static buffer to keep leftover data between calls. Reads in chunks of BUFFER_SIZE bytes (compile-time configurable), splits on \n, and returns one line at a time.
cc -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c main.cC Static Variables File Descriptors Memory Management Buffer Management
This repository is shared for educational and portfolio purposes only.
For 42 students:
- Do not copy this code for your own project submissions
- Use it as a reference to understand concepts and approaches
- Write your own original solution — that is the only way to actually learn
42's core values are learning through practice, peer collaboration, and genuine problem-solving.
MIT — see LICENSE for details. Copyright (c) 2026 Eneko Muñoz Bordona