Skip to content

leandrobbj/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Get Next Line  language 42

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

Get_Next_Line reads a text file from a file descriptor, one line at a time, until the end of the file is reached. The goal is to return the next available line on each call while keeping any unread data available for the next invocation. This project introduces the use of static variables in C to preserve state between function calls.

  1. Algorithm
  2. Instructions
  3. Resources
  4. AI Disclosure
  5. Final Notes

Algorithm

The core of the algorithm is a static char buffer[BUFFER_SIZE + 1]. Static works as a cache that stores any leftover data read from a file descriptor that was not part of the previously returned line.

Algorithm Flow

  • Initialization: get_next_line first checks whether the static buffer already contains leftover data from a previous call. If it does, that content is appended to the current line using join_free_line.

  • Reading: If the line is not complete yet, find_line keeps calling read() to fill the buffer. After each read, the new data is concatenated to the line until a \n is found or the end of the file is reached.

  • Cleanup: Once reading is complete, clean_line finds the newline with ft_strchr and terminates the returned string there. If extra data remains after the newline, it is shifted to the front of the static buffer so it can be used on the next call to get_next_line.

Instructions

The project must be compiled with the -D BUFFER_SIZE=n flag to define the buffer size used by read(). You can change BUFFER_SIZE as needed.

  • Compiling:

    cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c your_main.c -o program_name
  • Execution:

      ./program_name
    

*your_main and program_name are only reference names for your own files

Resources

AI Disclosure

In accordance with the 42 curriculum guidelines, AI tools focusing help my understanding of fundamental concepts such as static variables and file descriptor management and summarize the behavior of the function read().

The AI also helped with test development to identify potential logic bugs and edge cases during the implementation of complex functions, and supported the organization and formatting of project configuration files.

Final Notes

Developing get_next_line was a significant challenge that introduced me to the power of static variables for data persistence. Using a fixed-size buffer allowed for an efficient algorithm that minimizes memory fragmentation and prevents leaks.

This project also taught me to manage dynamic line growth and file descriptors, ensuring that every call reads only what is necessary to return a complete line. Ultimately, it was about building a deeper understanding of memory and logic to create a robust tool for my future projects at 42.

About

A C function that returns a line read from a file descriptor, utilizing static variables to maintain state between calls.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages