Skip to content

May it be a file, stdin, or even later a network connection, you will always need a way to read content line by line. It is time to start working on this function, which will be essential for your future projects.

Notifications You must be signed in to change notification settings

42-adbouras/get_next_line-1337MED

Repository files navigation

I - Mandatory Part

The purpose of the function get_next_line() is to read a line from a file descriptor and return it. The function reads the file line by line, which means that each call to get_next_line returns the next line in the file. Here's a step-by-step explanation of how the code works:

  • get_next_line is the main function. It takes a file descriptor fd as input. It has a static variable line which retains its value between function calls. This is used to keep track of the remaining part of the file to be read.

  • ft_read is called to read from the file descriptor fd into line. It reads up to BUFFER_SIZE characters at a time. If it encounters a newline character ('\n'), it stops reading. If an error occurs during reading, it frees cup and line and returns NULL.

  • If line is NULL or an empty string after the call to ft_read, get_next_line frees line and returns NULL.

  • ft_substr_ft is called to get the substring of line up to and including the first newline character. This is the line to be returned by get_next_line.

  • ft_save_static is called to get the remaining part of line after the first newline character. This is saved in line for the next call to get_next_line.

  • get_next_line returns the line read from the file.

  • ft_substr_ft and ft_save_static both allocate memory for the strings they return. It's the responsibility of the caller to free this memory when it's no longer needed.

  • If any of the memory allocations fail, the functions return NULL.
 This is a high-level overview of how the code works. The details of how each function works can be found in their respective definitions.

II - Bonus Part

Basicly the same workflow the only defference is that get_next_line(), it has a static char array line of size 10240 (number of file descriptors you can open in one proscess). This array is used to store lines read from different file descriptors.

Notice

⚠️ There is some minor mistakes but it still works! never gonna forget the evaluation for this project 🤦‍♂️

  • I let you find em!

About

May it be a file, stdin, or even later a network connection, you will always need a way to read content line by line. It is time to start working on this function, which will be essential for your future projects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages