My solution for the get_next_line assignment.
0

Configure Feed

Select the types of activity you want to include in your feed.

C 82.0%
Makefile 18.0%
51 1 0

Clone this repository

https://tangled.org/gm0stache.eu/ft-gnl https://tangled.org/did:plc:fom7cbhh4elo5hezvmhvezks
git@tangled.org:gm0stache.eu/ft-gnl git@tangled.org:did:plc:fom7cbhh4elo5hezvmhvezks

For self-hosted knots, clone URLs may differ based on your setup.



README.md

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


Description#

get_next_line is a 42 project designed to implement a function that reads a file line by line. The goal is to create a function that returns the next line of a file descriptor each time it is called, without losing the previous content. This project reinforces understanding of file descriptors, static variables, dynamic memory allocation, and buffer management in C.

The function must:

  • Read a line from a file descriptor.
  • Return the line (including the newline character \n if present).
  • Work with both regular files and standard input.

Project Structure#

ft_get_next_line/
├── get_next_line.c
├── get_next_line.h
├── get_next_line_utils.c
├── README.md

Instructions#

Create a main.c in the project directory:

#include "get_next_line.h"
#include <fcntl.h>
#include <stdio.h>

int main(void) {
    int fd = open("test.txt", O_RDONLY);
    char *line;

    while ((line = get_next_line(fd)) != NULL) {
        printf("%s", line);
        free(line);
    }
    close(fd);
    return (0);
}

now run the program with:

cc -Wall -Wextra -Werror -o test get_next_line.c get_next_line_utils.c main.c

Algorithm Explanation & Justification#

Core Algorithm#

The get_next_line function uses a buffered reading approach with a static character array to preserve state between calls.

Key Components:

  1. Static Buffer Storage:

    • A static character array buf[BUFFER_SIZE] stores leftover content between calls
    • This avoids the need for dynamic allocation of the stash itself
    • Content is copied to/from a temporary buffer (tmp_buf) for processing
  2. Buffered Reading:

    • Reads chunks of BUFFER_SIZE bytes using the read() system call
    • Accumulates content in tmp_buf until a newline is found or EOF is reached
    • Uses ft_strjoin to concatenate the static buffer content with newly read data
  3. Line Extraction (get_line function):

    • Searches for the first \n in the accumulated buffer
    • If found: extracts the line (including \n), saves remaining content back to the static buffer
    • If not found and EOF reached: returns the remaining content as the last line
    • Properly handles empty buffers and null pointers
  4. Memory Management:

    • All returned lines are dynamically allocated and must be freed by the caller
    • Uses free_val helper to safely free and nullify pointers
    • Clears the static buffer with ft_bzero on error conditions

Why This Approach?#

  • Efficiency: Minimizes system calls by reading in chunks (BUFFER_SIZE)
  • Memory Safety: Static buffer avoids repeated malloc/free for the stash; only the returned line and temporary buffers are dynamically allocated
  • Simplicity: The static array approach reduces complexity compared to managing a dynamically allocated stash

Resources#

Documentation & Tutorials#

AI Usage#

  • Debugging: Used AI to identify edge cases (e.g., files without \n, very long lines)
  • Algorithm Validation: Verified the logic for handling buffer sizes and edge conditions
  • Code Optimization: Suggested improvements for memory management and error handling