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!
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 |
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
- Work with
open
,read
,close
- Understand
stdin
,stdout
,stderr
- 🔗 GeeksforGeeks - File Descriptors
- Use a static buffer (stash) per FD to track leftover data
- Handle partial reads and newline detection
- Use
malloc
,free
,strjoin
,strdup
correctly - Clean up to avoid memory leaks
Recreate or reuse:
strchr
→ Find\n
strdup
→ Duplicate memorystrjoin
,substr
→ Combine & extract string parts
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 |
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 |
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 |
[read(fd) → BUFFER] → [append to stash]
↓
[find newline] → [split stash]
↓
[return line] + [keep leftover in static stash]
Tip: Use
valgrind
orleaks
to verify no memory is left unfreed!
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!).
- 42 Project Docs
- Repo: ayogun
- Repo: nickdotht
- Medium: Deep Dive #1
- Medium: Deep Dive #2
- YouTube: Line by Line
- 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!)
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.