Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

Repository files navigation

This project has been created as part of the 42 curriculum by jtravanc.

Description

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.

Algorithm Explanation

The solution uses a static variable buffer stream to persist data across multiple function calls.

  1. Reading Loop: The function uses a temporary stack buffer of size BUFFER_SIZE to read chunks of data from the file descriptor using the read() system call.
  2. Accumulation: The chunks are appended to a static pointer (storage) using a custom ft_strjoin_mod. This loop continues until a newline character (\n) is found within storage or the file reaches its end.
  3. Extraction & Trimming: Once a newline is present, ft_line_trim isolates and allocates memory for the single line to be returned (including the \n character if present).
  4. State Cleanup: Finally, ft_update_leftover cleans up the static storage, keeping only the remaining unread text for the next call, and freeing the old reference to prevent any memory leaks.

Instructions

Compilation

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages