This project has been created as part of the 42 curriculum by jtravanc.
The get_next_line project is a core assignment in the 42 curriculum designed to create a function that reads a line from a file descriptor. When called in a loop, get_next_line allows the user to read the contents of a file descriptor one line at a time until the End of File (EOF) is reached.
The primary goal of this project is to learn how a functional programming architecture handles dynamic memory allocation and static variables, managing data efficiently without knowing the size of the line beforehand.
The solution uses a static variable buffer stream to persist data across multiple function calls.
- Reading Loop: The function uses a temporary stack buffer of size
BUFFER_SIZEto read chunks of data from the file descriptor using theread()system call. - Accumulation: The chunks are appended to a static pointer (
storage) using a customft_strjoin_mod. This loop continues until a newline character (\n) is found withinstorageor the file reaches its end. - Extraction & Trimming: Once a newline is present,
ft_line_trimisolates and allocates memory for the single line to be returned (including the\ncharacter if present). - State Cleanup: Finally,
ft_update_leftovercleans up the staticstorage, keeping only the remaining unread text for the next call, and freeing the old reference to prevent any memory leaks.
To compile the code along with your main tester file, you need to define the BUFFER_SIZE macro. For example:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c main.c -o gnl