This project is part of the curriculum at Ecole 42. The goal of "42_get_next_line" is to create a function that reads a line from a file descriptor.
The get_next_line
function reads a line from a file descriptor. A "line" is defined as a succession of characters that end with a newline ('\n') or EOF (End of File) from the input file.
The function allocates memory for the line read, which must be freed by the caller when it is no longer needed. It returns 1 if a line has been read, 0 if the reading is complete, and -1 in case of an error.
- Install.
git clone https://github.com/cypri1-dev/42_get_next_line.git
cd 42_get_next_line
- To use the
get_next_line
function, include the header fileget_next_line.h
in your project. Then, call the function with the file descriptor from which you want to read a line.
#include "get_next_line.h"
int main(void)
{
char *line;
int fd;
int ret;
fd = open("example.txt", O_RDONLY);
if (fd == -1)
return (1);
while ((ret = get_next_line(fd, &line)) > 0)
{
printf("%s\n", line);
free(line);
}
if (ret == -1)
return (1);
close(fd);
return (0);
}