Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

πŸ“– Get Next Line

A 42 School project - Reading lines from file descriptors made simple


C 42 School License Last Commit


πŸ“‘ Table of Contents


🎯 Overview

Get Next Line is a fundamental project from 42 School that challenges students to create a function capable of reading a line from a file descriptor, regardless of buffer size. This project deepens understanding of:

  • Static variables in C
  • Dynamic memory allocation
  • File descriptor operations
  • Buffer management

The goal is to implement a function that returns one line at a time from a file descriptor, handling various buffer sizes efficiently.


✨ Features

πŸ“Œ Mandatory Part

  • βœ… Read and return one line at a time from any file descriptor
  • βœ… Handle different BUFFER_SIZE values (compile-time parameter)
  • βœ… Proper memory management with no leaks
  • βœ… Works with files, stdin, and other file descriptors
  • βœ… Returns line including the newline character \n (if present)
  • βœ… Returns NULL when EOF is reached or an error occurs

🌟 Bonus Part

  • βœ… Manage multiple file descriptors simultaneously
  • βœ… Each file descriptor maintains its own reading position
  • βœ… Switch between different file descriptors without losing progress

πŸ“‚ Project Structure

GetNextLine/
β”œβ”€β”€ get_next_line.c              # Main function implementation
β”œβ”€β”€ get_next_line.h              # Header file with prototypes
β”œβ”€β”€ get_next_line_utils.c        # Helper functions
β”œβ”€β”€ get_next_line_bonus.c        # Bonus: multi-fd support
β”œβ”€β”€ get_next_line_bonus.h        # Bonus header
β”œβ”€β”€ get_next_line_utils_bonus.c  # Bonus utilities
β”œβ”€β”€ get_next_line.pdf            # Project subject (42 School)
└── test.txt                     # Sample test file

πŸ“„ File Descriptions

File Description
get_next_line.c Core implementation with read_and_stash, extract_line, and clean_stash functions
get_next_line.h Header with function prototypes and BUFFER_SIZE definition
get_next_line_utils.c Utility functions: ft_strlen, ft_strchr, ft_strjoin
get_next_line_bonus.c Enhanced version supporting multiple file descriptors with array of static variables
get_next_line_bonus.h Bonus header with OPEN_MAX definition
get_next_line_utils_bonus.c Bonus utility functions (identical to mandatory)
get_next_line.pdf Official 42 project subject with requirements
test.txt Sample text file for testing ("Petit ours brun" lyrics)

πŸš€ Getting Started

Prerequisites

  • GCC or any C compiler
  • Make (optional)
  • Unix-based system (Linux, macOS) or WSL

Installation

  1. Clone the repository:
git clone https://github.com/nicoco23/GetNextLine.git
cd GetNextLine

Compilation

Standard Version

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

Bonus Version

cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line_bonus.c get_next_line_utils_bonus.c main_bonus.c

With Different Buffer Sizes

# Small buffer (slower, more read() calls)
cc -Wall -Wextra -Werror -D BUFFER_SIZE=1 get_next_line.c get_next_line_utils.c main.c

# Large buffer (faster, fewer read() calls)
cc -Wall -Wextra -Werror -D BUFFER_SIZE=10000 get_next_line.c get_next_line_utils.c main.c

Usage

Basic Example

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

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

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

Bonus Example (Multiple File Descriptors)

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

int main(void)
{
    int     fd1, fd2, fd3;
    char    *line;

    fd1 = open("file1.txt", O_RDONLY);
    fd2 = open("file2.txt", O_RDONLY);
    fd3 = open("file3.txt", O_RDONLY);

    // Read alternately from different files
    line = get_next_line(fd1); // Line 1 from file1
    free(line);
    line = get_next_line(fd2); // Line 1 from file2
    free(line);
    line = get_next_line(fd1); // Line 2 from file1 (keeps position!)
    free(line);

    close(fd1);
    close(fd2);
    close(fd3);
    return (0);
}

πŸ§ͺ Testing

Run Included Tests

# Test standard version
./a.out

# Expected output: Contents of test.txt printed line by line

Test with Different Buffer Sizes

# Test with BUFFER_SIZE=1
cc -Wall -Wextra -Werror -D BUFFER_SIZE=1 *.c && ./a.out

# Test with BUFFER_SIZE=9999
cc -Wall -Wextra -Werror -D BUFFER_SIZE=9999 *.c && ./a.out

Memory Leak Testing

# Compile with debug symbols
cc -g -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c main.c

# Run with valgrind
valgrind --leak-check=full --show-leak-kinds=all ./a.out

Recommended External Testers


βš™οΈ How It Works

Architecture

The function is divided into three main operations:

  1. read_and_stash: Reads from fd and accumulates data in a static buffer until \n is found or EOF
  2. extract_line: Extracts one complete line from the stash (including \n)
  3. clean_stash: Removes the extracted line from stash, keeping remaining data for next call

Visual Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  File: "Hello\nWorld\nTest"                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                          β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  read_and_stash(fd, stash)     β”‚  ← Read BUFFER_SIZE bytes
         β”‚  Accumulates: "Hello\nWorld\n" β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                          β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  extract_line(stash)           β”‚  ← Extract until \n
         β”‚  Returns: "Hello\n"            β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                          β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  clean_stash(stash)            β”‚  ← Keep remaining data
         β”‚  Stash now: "World\n"          β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ’‘ Key Concepts

πŸ“Œ Static Variables

static char *stash;  // Mandatory: single static variable
  • Preserves data between function calls
  • Retains remainder after extracting a line

🌟 Bonus: Static Array

static char *stash[OPEN_MAX];  // Bonus: array of stashes
  • Each file descriptor has its own stash
  • Allows reading from multiple files simultaneously

πŸ”§ BUFFER_SIZE

The BUFFER_SIZE defines how many bytes are read per read() call:

  • Small BUFFER_SIZE (1-10): More system calls, slower but works
  • Large BUFFER_SIZE (1000+): Fewer system calls, faster
  • Does NOT affect the output: Always returns complete lines

Example with BUFFER_SIZE=5 reading "Hello World\n":

  1. Read: "Hello" β†’ no \n, store in stash
  2. Read: " Worl" β†’ no \n, append to stash
  3. Read: "d\n" β†’ found \n, return "Hello World\n"

πŸŽ“ Learning Outcomes

This project teaches essential C programming concepts:

  • βœ… Memory Management: Dynamic allocation, avoiding leaks, proper freeing
  • βœ… Static Variables: Understanding scope and lifetime
  • βœ… File I/O: Working with file descriptors and read() system call
  • βœ… Buffer Management: Handling data that doesn't fit in a single read
  • βœ… Edge Cases: EOF, empty lines, invalid fd, read errors
  • βœ… Code Organization: Modular design with helper functions

πŸ“ License

This project is part of the 42 School curriculum and follows their academic guidelines.


πŸ‘€ Author

Nicolas Tassin (ntassin)


⭐ Don't forget to star this repo if it helped you!

Back to Top

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages