get_next_line is a project developed for 42 Heilbroon School.
C programming - read() - file descriptor - buffer - static variable
In this project I had to create a function which read()
from a text file everytime is called, line by line. For more info about the requirements, take a look at the subject contained in this repo.
The project covers the following topics:
As suggested by the subject, the use of a static variable static char *line
is a way to remember the line returned every time the function is called. Their use involve static memory allocation, which extends their lifetime through the entire run of the program.
To properly use read()
it is necessary to understand this basic concept. In general, a buffer is a temporary storage area, where the data is hold to be moved, processed or transferred (think about the data transferred in a video streaming service).
Like in any other project of 42, the memory allocated by malloc()
has to be properly sfreed. No leaks are tolerated. In this project I checked the leaks using valgrind and system("leaks <program-name>")
In the bonus part of the project the goal is implement a version of get_next_line
which can manage multiple file descriptors at the same time remembering the current line of each file, so that everytime the function is called, returns the correct next line of each one.
- Clone the repository:
git clone [email protected]:NicoCastelnuovo/get_next_line.git get_next_line
- Navigate to the
get_next_line
directory andmake
the archive, ormake bonus
for more funcionalities:
cd get_next_line
make
- To use the function in inside your .c file, you have to include the header on the top of it:
#include "get_next_line.h"
- Compile your executable. you can add the optional argument
-D BUFFER_SIZE=n
to change the size of the buffer whichread()
uses to read from the file:
cc <your_main.c> ./<gnl_path>/libgnl.a -I ./<gnl_path>/get_next_line.h -o <program_name> [-D BUFFER_SIZE=n]
Note: if you need to
open()
the file andread()
, remember to# include <fcntl.h>
header inside your project!
- Enjoy!