Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cypri1-dev/42_get_next_line

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.
https://github.com/cypri1-dev/42_get_next_line

bonus get-next-line get-next-line-42 getnextline getnextline-42 getnextline42 mandatory

Last synced: 28 days ago
JSON representation

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.

Awesome Lists containing this project

README

        

##

## Description
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.

## Usage
1. Install.
```bash
git clone https://github.com/cypri1-dev/42_get_next_line.git
cd 42_get_next_line
```
2. To use the `get_next_line` function, include the header file `get_next_line.h` in your project. Then, call the function with the file descriptor from which you want to read a line.

## Example Usage
```c
#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);
}
```