Skip to content

HajerZam/C-Project-Get_Next_Line

Repository files navigation

🚀 Get_Next_Line

My second project in 42 Common Core — let's goooooo (๑•̀ ᴗ•́)۷✧
A function that reads from a file descriptor, line by line, with dynamic memory management!


📊 Project Overview

Metric Details
📁 Project Name get_next_line
🧠 Key Concepts File descriptors, buffers, dynamic memory
🧩 Bonus Features Support for multiple FDs
⏱️ Time Spent ~2 weeks
🔥 Difficulty Medium – High

🎯 Goals

Build a robust version of:

char *get_next_line(int fd);

That will:

  • ✅ Read and return one line at a time
  • ✅ Work with any valid file descriptor
  • ✅ Handle multiple FDs (bonus)
  • ✅ Use dynamic memory — no fixed-size line limits
  • ✅ Preserve remaining buffer data between calls

📚 Core Concepts & Skills

🔧 File Descriptors & Low-Level I/O

🧵 Buffers & Persistent State

  • Use a static buffer (stash) per FD to track leftover data
  • Handle partial reads and newline detection

🧠 Dynamic Memory

  • Use malloc, free, strjoin, strdup correctly
  • Clean up to avoid memory leaks

🧰 String Utilities

Recreate or reuse:

  • strchr → Find \n
  • strdup → Duplicate memory
  • strjoin, substr → Combine & extract string parts

⚙️ Project Flow (How it works)

get_next_line(fd)
    ↓
[Read BUFFER_SIZE bytes]
    ↓
[Append to stash]
    ↓
[Search for newline '\n']
    ↓
[Extract line + update stash]
    ↓
Return line
Step Description
✅ Validate input Check for valid fd and buffer size
📥 Read from fd Until newline or EOF
💾 Save remainder Static stash retains leftover data
🧻 Extract line Up to and including \n
🔚 Handle EOF Return NULL when done

🔧 Helper Functions

Function Purpose
ft_strchr Locate \n in stash
ft_strdup Duplicate a string
ft_strjoin_free Join stash and buffer, free old stash
ft_substr Extract a substring

🧪 Example Execution

Input file:

Hello\nWorld\n42\n
Call # Returned Line Stash After Call
1 Hello\n World\n42\n
2 World\n 42\n
3 42\n `` (empty)
4 NULL EOF reached

🔄 Memory & Flowchart

[read(fd) → BUFFER] → [append to stash]
       ↓
[find newline] → [split stash]
       ↓
[return line] + [keep leftover in static stash]

Tip: Use valgrind or leaks to verify no memory is left unfreed!


🔍 Bonus: Multiple FDs

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

get_next_line(fd1); // reads from file1
get_next_line(fd2); // independently reads from file2

Your implementation must track separate stash buffers for each FD. Use something like a static array indexed by FD, or a custom linked list (if you're daring!).


📦 Resources


✅ Checklist

  • Reads 1 line at a time (with \n)
  • Handles files of any size
  • Supports multiple FDs (bonus)
  • Uses only allowed functions
  • No memory leaks
  • Makefile with all required rules
  • Clean, modular code (helpers, no spaghetti!)

💬 Final Thoughts

get_next_line taught me how to:

  • Think like the OS (I/O is tricky!)
  • Work with static memory across function calls
  • Build resilient and reusable logic in C
  • Handle edge cases and memory carefully

Master this project, and buffer logic will never scare you again 😤


Made with 💻, ⚔️ and lots of malloc debugging.


About

my second project in 42 common core let's goooooo (๑•̀ ᴗ•́)૭✧

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages