This project has been created as part of the 42 curriculum by vneves-c.
Get Next Line is a project from the 42 curriculum that introduces the concept of static variables in C. The goal is to program a function that returns a line read from a file descriptor. This function allows you to read a text file one line at a time, regardless of the BUFFER_SIZE used during reading.
To compile the project, you need to use a C compiler (like cc) and include the necessary source files (get_next_line.c and get_next_line_utils.c). You must also define the buffer size used by the read() function using the -D BUFFER_SIZE=n flag.
Example compilation command:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 -c get_next_line.c get_next_line_utils.cOnce compiled, repeated calls to the get_next_line(int fd) function will return consecutive lines from the specified file descriptor.
The implementation relies on a single static variable, stash, to preserve data read from the file descriptor across multiple calls to the function.
- Reading: The function reads data from the file descriptor in chunks of
BUFFER_SIZEbytes and appends it to thestash. This loop continues until a newline character (\n) is found within thestashor the end of the file is reached. - Extraction: Once a newline is detected (or EOF is reached), the function
ft_extract_lineisolates the string from the beginning of thestashup to and including the newline character. - Saving the Rest: The remaining characters in the
stash(those following the newline) are preserved by theft_save_restfunction for the next timeget_next_lineis called. - Memory Management: Helper functions like
ft_strjoindynamically allocate memory for strings as they grow, and temporary variables are carefully freed to avoid memory leaks.
man 2 readman 3 mallocman 3 free- AI Usage: AI was used exclusively to assist in drafting this README file content.