Development repository for the 42cursus' get_next_line project
For more about 1337 Coding School and its projects, feel free to connect with me on LinkedIn.
I successfully completed the get_next_line
project, including the bonus part handling multiple file descriptors, with an exceptional score!
(The code I provided is fixed so the final grade for it would be 125/100).
The get_next_line
function reads a file line by line, handling various file descriptors efficiently.
char *line;
while ((line = get_next_line(fd)))
{
printf("%s", line);
free(line);
}
- Reads one line at a time (including
\n
) - Efficient buffer management
- Handles multiple file descriptors (Bonus Part ✅)
- Works on any file, including stdin
- Uses a static buffer to store leftover data between function calls.
- Reads the file chunk by chunk (defined by
BUFFER_SIZE
). - Extracts and returns a single line on each call.
- Frees memory properly to prevent leaks.
Function | Description |
---|---|
get_next_line(int fd) |
Reads and returns the next line from fd |
ft_strjoin(s1, s2) |
Concatenates two strings |
ft_strchr(s, c) |
Finds character c in string s |
ft_strdup(s) |
Duplicates a string |
ft_substr(s, start, len) |
Extracts a substring |
The bonus part extends get_next_line
to handle multiple file descriptors simultaneously, ensuring:
- Independent reading from different files
- No data loss between function calls
- Smooth performance in multi-file environments
This project includes a Makefile
for easy compilation.
Run the following command to compile:
make
This will generate get_next_line.a
(static library).
To use get_next_line
, include the header and link the library:
#include "get_next_line.h"
Compile your program with:
gcc main.c get_next_line.a -o my_program
To remove object files and binaries:
make clean # Removes object files
make fclean # Removes object files and library
make re # Cleans and recompiles everything
📚 Documentation & References
🛠 Technical Articles & Discussions
Happy coding! 🚀