- Project Description
- How It Works
- Compilation
- Usage
- Expected Output
- Buffer Size Configuration
- Bonus Part
- Project Structure
- License
- Contact
get_next_line is a function that reads a line from a file descriptor, returning it one line at a time — each call continues from where the last one left off.
This project focuses on understanding file I/O in C, managing memory safely, and handling static variables across function calls.
🧠 This project was built to strengthen my low-level programming skills in C by implementing a custom line-reading function using file descriptors and dynamic memory management.
- Reads from a given file descriptor (
fd
) until a newline\n
or EOF is found. - Returns a dynamically allocated string for each line.
- Manages internal buffering and memory for multiple function calls.
- Uses a static variable to retain state between reads.
char *get_next_line(int fd);
Clone the repository and compile with your desired buffer size:
git clone https://github.com/valyriasteel/get_next_line.git
cd get_next_line
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c -o gnl
Include the header and call the function:
#include "get_next_line.h"
#include <fcntl.h>
#include <stdio.h>
int main() {
int fd = open("file.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)) != NULL) {
printf("%s", line);
free(line);
}
close(fd);
return 0;
}
🧪 You can test
get_next_line
using your ownmain.c
or compare its output with known test files.
Assume file.txt
contains:
Hello
World!
Test123
The output will be:
Hello
World!
Test123
The BUFFER_SIZE
is defined via compiler flags:
-D BUFFER_SIZE=42
You can adjust it during compilation:
cc -D BUFFER_SIZE=128 ...
The bonus version supports:
-
Multiple file descriptors: reading from several files in parallel
-
Bonus files:
get_next_line_bonus.c
get_next_line_bonus.h
get_next_line_utils_bonus.c
Compile bonus version like this:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line_bonus.c get_next_line_utils_bonus.c -o gnl_bonus
Use it the same way as the main version.
All files are located in the root directory:
get_next_line/
├── get_next_line.c # Main implementation
├── get_next_line_utils.c # Helper functions
├── get_next_line.h # Header file
├── get_next_line_bonus.c # Bonus version
├── get_next_line_utils_bonus.c # Bonus helpers
├── get_next_line_bonus.h # Bonus header
└── README.md # Documentation
This project is licensed under the MIT License. See LICENSE for details.
For questions, issues, or contributions:
- GitHub: valyriasteel
- Feel free to open an issue or pull request!
📝 This project and its documentation are written in English to ensure accessibility for a global audience.
Keywords: get_next_line, 42 school project, file descriptor, C programming, memory handling, line reader