This project has been created as part of the 42 curriculum by .
Get Next Line is a core project of the 42 School curriculum.
The objective of this project is to implement a function, get_next_line, that reads a file descriptor line by line, returning one line per function call.
The function must:
- Read from a file descriptor until a newline character (
\n) or End Of File (EOF) is reached. - Preserve unread data between calls.
- Allocate memory dynamically for each returned line.
- Be robust against different buffer sizes and input sources.
The bonus part extends this behavior to support multiple file descriptors simultaneously, ensuring that reading from one file descriptor does not interfere with another.
char *get_next_line(int fd);Returns a line ending with \n if present, or NULL if EOF is reached and no data remains or an error occurs.
get_next_line.c and get_next_line_bonus.c contain COMMENTED main funtions to test them.
Uncomment them and compile the project with your own files or test files:
cc -Wall -Wextra -Werror get_next_line.c get_next_line_utils.cFor the bonus version:
cc -Wall -Wextra -Werror get_next_line_bonus.c get_next_line_utils_bonus.cTo test them just execute the a.out file with file name (or few file names if it is bonus) as arg to read them.
./a.out get_next_line.cYou may define BUFFER_SIZE at compile time:
cc -D BUFFER_SIZE=42 get_next_line.c get_next_line_utils.cint fd = open("example.txt", O_RDONLY);
char *line;
while ((line = get_next_line(fd)) != NULL)
{
printf("%s", line);
free(line);
}
close(fd);-
Buffered Reading
- Use
read()to fetch chunks of data of sizeBUFFER_SIZE. - If
read()returned negative, it immidiatly skips the reading part, frees everything and returnsNULL. - Else: append each chunk (
stash_expandfunction) to a persistent buffer called the stash and repeat it untilwas_read == BUFFER_SIZEandbufferdoesnt contain\n.
- Use
-
Line Extraction
- If
read()returned integer more then 0, it usesstach_extract_lineis used to extract one line from stash and leave other chars in it. if it returned full stash, stash will be set toNULL. - If
read()returned 0 (EOF) and the stash contains data, return the remaining data as the final line. - if it returned negative, it will just remove everything and return
NULL
- If
-
Memory Safety
- All returned lines (or
t_fd_listin bonus) are dynamically allocated. - All allocated memotry is freed when no longer needed.
- All returned lines (or
Bonus part works the same way, but it uses static t_fd_list with stashes and 3 additional methods instead of stash
typedef struct s_fd_list_node
{
int fd;
char *stash;
struct s_fd_list_node *next;
} t_fd_list;| Column 1 | Column 2 |
|---|---|
add_node |
Adds node with exact fd and returns it. If this node already exists, it will just return it |
remove_node |
Used to free the node by its fd |
node_by_fd |
Returns node with exact fd if it exists, otherwise NULL |
This approach guarantees:
- No data loss between calls
- Minimal reads from the file descriptor
- Correct handling of partial lines
man readman openman close- 42 School subject PDF for get_next_line
AI tools were used only for assistance, including:
- Finding errors
- Writing basic README
All implementation logic, coding, testing, and final decisions were made by the project author in compliance with 42 rules.