This project has been created as part of the 42 curriculum by mkhoubaz
Get Next Line is a C project that consists of implementing a Get_next_line function.
The goal of this function is to read from file descriptor and return one line at atime, ending with new line character (\n) if present, or NULL when nothing left to read ot an error occurs.
The project focuses on:
- Using the
read()system call - Managing memory safely (no leaks)
- Handle static variables
- Supporting different
BUFFER_SIZEvalue - Managing multiple file descriptor in Bonus part
Use CC compiler with the required flags:
cc -Wall -Wextra -Werror get_next_line.c get_next_line_utils.c main.c && ./a.outNote:
Flags to any warning be come error
Using customBUFFER_SIZE marco
cc -Wall -Wextra -Werror get_next_line.c get_next_line_utils.c main.c -D BUFFER_SIZE=1337 && ./a.outTo verify memory leaks use Valgrind
cc -Wall -Wextra -Werror get_next_line.c get_next_line_utils.c main.c -D BUFFER_SIZE=42 && valgrind -s --track-origins=yes --leak-check=full ./a.outNote:
-s for summary error report --track-origins=yes to track uninitialized values --leak-check=full provides detailed information
File main example to test Mandatory part
#include "get_next_line.h"
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
int main()
{
int fd = open("text.txt", O_RDONLY);
char *line;
line = get_next_line(fd);
printf("%s", line);
free(line);
line = get_next_line(fd);
printf("%s", line);
free(line);
close(fd);
}File main example to test Bonus part (Multiple file descriptor)
#include "get_next_line_bonus.h"
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
int main()
{
int fd = open("text.txt", O_RDONLY);
int fd2 = open("text2.txt", O_RDONLY);
int fd3 = open("text3.txt", O_RDONLY);
char *line;
line = get_next_line(fd);
printf("%s", line);
free(line);
line = get_next_line(fd2);
printf("%s", line);
free(line);
line = get_next_line(fd2);
printf("%s", line);
free(line);
line = get_next_line(fd3);
printf("%s", line);
free(line);
line = get_next_line(fd2);
printf("%s", line);
free(line);
line = get_next_line(fd3);
printf("%s", line);
free(line);
line = get_next_line(fd);
printf("%s", line);
free(line);
close(fd);
}Static variables in C geeksforgeeks
read() system call documentation opengroup
File descriptor linux programming interface book
File offset and read behavior IC221 - Systems Programming and linux programming interface book
AI tool were used only to generated test file and simple input data.
- A static variable (
line) use to store leftover data between calls. - Memory is allocate for a buffer (
buf) of sizeBUFFER_SIZE+ 1 to store data read byread(). - The function read from file descriptor until:
- A newline (
\n) is found, or read()return0(end of file), or- An error occurs.
- A newline (
- Each buffer read is appended to static variable using a custom
ft_strjoin(). - When a newline detected:
- A substring from the beginning up to the newline using
ft_substr()and returned. - The remaining data after the newline is kept in the static variable (
line) for the next call
- A substring from the beginning up to the newline using
- If the end of file is reached and no newline:
- The remaining content is returned.
- The static variable is freed using
free().
- All allocated memory is carefully freed to avoid leaks.
- Return one line at time from file descriptor
- Handle macro
BUFFER_SIZE - No memory leaks
- Bound: support multiple file descriptor
- Handle when
malloc()andread()filed