Skip to content

kaasiel/get_next_line

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

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

get_next_line

Description

get_next_line is a C function that reads a line from a file descriptor, one line at a time. Each call to the function returns the next line from the file descriptor, including the terminating \n character (except at end of file if no trailing newline exists). It returns NULL when there is nothing left to read or if an error occurs.

The project introduces a key concept in C programming: static variables, which allow a function to retain state between successive calls.

Files

File Description
get_next_line.c Core function implementation
get_next_line_utils.c Helper functions
get_next_line.h Header file with function prototype

Bonus files:

  • get_next_line_bonus.c
  • get_next_line_utils_bonus.c
  • get_next_line_bonus.h

Instructions

Compilation

cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c

The BUFFER_SIZE macro defines how many bytes are read at a time. You can adjust this value freely. The project must compile with or without the -D BUFFER_SIZE flag.

Usage example

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

int main(void)
{
    int     fd;
    char    *line;

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

Algorithm

The function uses a static buffer (leftover) approach:

  1. A static char * variable retains any data read beyond the last newline between calls.
  2. On each call, the function first checks the leftover for a newline. If one is found, it extracts and returns the line without reading more from the file descriptor.
  3. If no newline is found in the leftover, it reads chunks of BUFFER_SIZE bytes from the file descriptor, appending each chunk to the leftover, until a newline is found or EOF is reached.
  4. Once a newline (or EOF) is detected, the line up to and including \n is extracted and returned. The remainder is saved back into the static variable for the next call.

Why this approach?
It minimizes unnecessary reads — the function reads only as much as needed per call. The static variable elegantly solves the problem of keeping unconsumed data between calls without using global variables or requiring the caller to manage state.

Bonus: For multiple file descriptor support, the static variable becomes an array indexed by file descriptor, allowing independent leftover tracking per fd.

Resources

AI Usage

AI was used to help structure and write this README file. The logic, implementation, and understanding of the project were developed independently through personal reasoning and peer exchanges, in accordance with the 42 AI guidelines.

About

The get_next_line project is a programming project that uses a function that reads from a file descriptor and returns one line at a time, effectively managing a static buffer to handle subsequent calls without losing data between reads.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages