{EPITECH.} first year project.
C function that reads from a filedescriptor line per line, each time the function is called.
char *get_next_line(int const fd);
-
get_next_line()
retrieves lines using theread(2)
syscall. -
The
READ_SIZE
macro located inside theget_next_line.h
header file represents the number of bytes that are read each timeread(2)
is called. -
You can edit this macro to optimized the number of calls to
read(2)
.
# define READ_SIZE (32)
If you don't know what to do, simply keep the default value.
Include get_next_line()
's header file inside your C source file:
#include "get_next_line.h"
- When calling
get_next_line()
, feed it a filedescriptor as a typeint
argument. - Each time you call
get_next_line()
, the function allocates & returns achar *
. - The first time you call
get_next_line()
, thechar *
returned contains the first line found when reading from the filedescriptor you fed to the function. - After the first call, each call to
get_next_line()
with the same filedescriptor will return the next line found. - When there is no more line to read,
get_next_line()
returnsNULL
. - If a fatal error occurs (
malloc(3)
orread(2)
failure),get_next_line()
also returnsNULL
.
Here is an example that reads and prints all the lines from stdin:
char *line;
while ((line = get_next_line(0)) != NULL)
printf("Next line -> %s", line);