The get_next_line project is designed to implement a function that reads a file line by line. The function should work with any file descriptor and must return each line, including the newline character (\n).
The function should:
- Read from a file descriptor (
fd) one line at a time. - Return
NULLwhen there is nothing left to read or if an error occurs. - Work with different
BUFFER_SIZEvalues set at compile time. - Handle both file input and standard input (
stdin). - Be implemented without using global variables or the
lseek()function.
get_next_line.c– Main function to read a line from a file descriptor.get_next_line_utils.c– Helper functions used inget_next_line.c.get_next_line.h– Header file containing function prototypes and necessary includes.
get_next_line_bonus.c– Modifiedget_next_lineto handle multiple file descriptors.get_next_line_utils_bonus.c– Helper functions for the bonus part.get_next_line_bonus.h– Header file for the bonus implementation.
You can compile the project with different BUFFER_SIZE values using:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.c -o gnlFor the bonus part:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=42 get_next_line_bonus.c get_next_line_utils_bonus.c -o gnl_bonusTo use the function in a test program:
#include <fcntl.h>
#include <stdio.h>
#include "get_next_line.h"
int main(void)
{
int fd = open("test.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)))
{
printf("%s", line);
free(line);
}
close(fd);
return 0;
}- Reads and returns one line at a time.
- Handles any
BUFFER_SIZEvalue. - Works with both files and standard input.
- The bonus version supports multiple file descriptors simultaneously.
This project follows the 42 School project requirements and is meant for educational purposes.