https://github.com/mbdanielcrespo/get_next_line
A function that reads a file and returns the next line on each subsequent call.
https://github.com/mbdanielcrespo/get_next_line
42school c-programming educational file-handling get-next-line line-reader
Last synced: 11 months ago
JSON representation
A function that reads a file and returns the next line on each subsequent call.
- Host: GitHub
- URL: https://github.com/mbdanielcrespo/get_next_line
- Owner: mbdanielcrespo
- Created: 2023-04-19T21:01:25.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2025-04-17T10:29:55.000Z (about 1 year ago)
- Last Synced: 2025-07-05T18:11:34.832Z (12 months ago)
- Topics: 42school, c-programming, educational, file-handling, get-next-line, line-reader
- Language: C
- Homepage:
- Size: 274 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ๐ Get Next Line




## ๐ Introduction
Get Next Line (GNL) is a crucial project at 42 School that challenges students to implement a function capable of reading a line from a file descriptor. This seemingly simple task introduces fundamental concepts in file I/O operations, memory management, and static variables in C.
> "Reading a line from a fd is way too tedious" - This project solves that problem by creating a convenient function you'll likely use in many future projects.
This function will be an essential tool throughout your journey at 42, as it enables reading from files, standard input, or even network connections line by line.
## ๐ฏ Project Objectives
- Create a function that returns a line read from a file descriptor
- Learn about static variables in C
- Understand file descriptors and basic I/O operations
- Master dynamic memory allocation and management
- Implement buffer handling for efficient file reading
- Develop code that works regardless of buffer size
## ๐ ๏ธ Implementation Details
### Core Function
```c
char *get_next_line(int fd);
```
Returns a line read from a file descriptor, with the newline character included.
### Files Structure
| File | Description |
|----------|-------------|
| **get_next_line.c** | Main function implementation |
| **get_next_line.h** | Header with function prototypes and definitions |
| **get_next_line_utils.c** | Helper functions for the main implementation |
### Key Components
| Function | Description |
|----------|-------------|
| **get_next_line** | Main function that returns the next line from a file descriptor |
| **ft_read_st_save** | Reads from file descriptor into buffer and appends to static variable |
| **ft_get_line** | Extracts a line from the saved content |
| **ft_st_save** | Updates the static variable by removing the line just extracted |
## ๐ก How It Works
1. **File Reading Process:**
- The function reads `BUFFER_SIZE` bytes at a time from the file descriptor
- Content is stored in a static variable to persist between function calls
- Reading continues until a newline character is found or EOF is reached
2. **Line Extraction:**
- When a newline is found, the function extracts everything up to and including the newline
- The extracted line is returned to the caller
- Remaining content stays in the static variable for the next call
3. **Memory Management:**
- Dynamic memory allocation for the buffer and returned line
- Careful freeing of memory to avoid leaks
- Handling of edge cases (empty file, read errors, etc.)
## ๐งช Testing
The function was tested with:
- Various buffer sizes
- Multiple file descriptors simultaneously
- Binary files, empty files, and large text files
- Standard input and custom file descriptors
## ๐ Skills Developed
- File I/O operations in C
- Static variables and their appropriate use cases
- Dynamic memory allocation and management
- Buffer manipulation techniques
- Defensive programming and error handling
- Writing clean, maintainable code
---
### ๐ Project Stats
| Metric | Value |
|--------|-------|
| Final Score | 100/100 |
| Functions Implemented | 4 |
| Lines of Code | ~150 |
| Completion Time | 1 week |