/get_next_line

This project is about programming a function that returns a line read from a file descriptor.

Primary LanguageCMIT LicenseMIT

This project is about programming a function that returns a line read from a file descriptor.

Project

This is the secound project of the common core at 42 São Paulo. The project wants that you code a function that read from a file descriptor and returns an allocated line. The returned line has a newline character at the end of the string followed by a null character.

The get_next_line works with all file descriptors, so it's also possible to read from the Standard Input, Standard Output and Standard Error.

Documentation

NAME

get_next_line - read a line from a file descriptor

SYNOPSIS

// MANDATORY: Supports only one file descriptor
#include  "get_next_line.h"

// BONUS: Supports one or more file descriptors at the same time
#include  "get_next_line_bonus.h"

PROTOTYPE

char    *get_next_line(int fd);

DESCRIPTION

get_next_line will read a line from a file descriptor and return an allocated string. If file descriptor is empty or EOF, a NULL will be returned. The line returned is allocated with malloc; the caller must free it when finished. The line returned has the final newline.

RETURN VALUE

get_next_line returns the text of the first line read from a file descriptor. A blank line returns a empty string. If EOF is encontered while reading a line, and the line is empty, NULL is returned.

How to test it?

Clone this repository:

git clone https://github.com/rgrmra/get_next_line.git get_next_line

Then compile the files as following:

Note

The main.c that I built is limited just to one file descriptor at time; but it's possible to use more file descriptors with the get_next_line bonus version.

Mandatory:

Reads from just one file descriptor.

cc -Wall -Wextra -Werror main.c get_next_line.c get_next_line_utils.c -o get_next_line

Bonus:

Reads from multiple file descriptors.

cc -Wall -Wextra -Werror main.c get_next_line_bonus.c get_next_line_utils_bonus.c -o get_next_line

Execution:

Reads from the standart output.

./get_next_line

Reads from a file.

./get_next_line example.txt

Important

The get_next_line function has a buffer size of 2048 bits, but is possible to redefine it.
Use the flag -D BUFFER_SIZE=<value> (eg. -D BUFFER_SIZE=10240) to redefine the buffer size.

cc -Wall -Wextra -Werror main.c get_next_line.c get_next_line_utils.c -o get_next_line -D BUFFER_SIZE=10240