Skip to content

tiagohtavares/get_next_line

Repository files navigation

get_next_line

About

This project consists of creating a function that returns the first line in a file(*.txt). A line is defined by ending in '\n' and '\n' has to be returned with the line. When the function is called several times in the same progam it must remember in wich line it left off.

This function must read the file in chunks defined by BUFFER_SIZE(size 10 by default) and allways return one line no matter the size of the buffer. Once all lines have been read it must return NULL.

Implementation

get_next_line takes in as arg the fd of the file to read and begins by alocating a temp char *buffer wich is the size of buffer. It continues to read n bytes the size of BUFFER_SIZE until it finds the end of the line '\n' and storing them on the global char *stash.

Once '\n' is found it will format the stash taking from it all the bytes before '\n' and putting them on char *line .It will then be returned and char *stash will remain with the leftover bytes from the file. char *stash must be allocated with exact ammount of bytes left.

This happens when BUFFER_SIZE does not perfectly read the line, leaving extra bytes after '\n' that cannot be lost and must be stored so get_next_line continues to properly return the entire line once it's called again.

Bonus

For bonus part get_next_line must be able to read multiple files at the same time without loosing the reading thread of each one.

This is done by making global var *stash a double pointer var **stash. This allows us to create an array in wich the "index" corresponds to the specific file being read allowing us to have one "stash" for each file.

Use

This project will be added to libft for further use in other work.

Releases

No releases published

Packages

No packages published

Languages