A function that reads a file line-by-line.
What can I say that @harm-smits hasn't said already?
get_next_line
is a function that reads an entire line
from a file indexed by a file descriptor fd
.
It then alocates a string with the contents
of that line without the linebreak '\n'
and points line
to it.
We don't have to pass anything allocated to get_next_line
,
we just pass the address of a pointer that will point to the allocated string.
line
should be freeable with free()
after the function call
unless an error occured.
We will incrementally read the file with read()
,
which advances its position in the file dexample escriptor automatically.
We need to use a static pointer as a read buffer to access what was read in previous calls.
We need to handle the following situations:
- If the read buffer doesn't have a
'\n'
, we concatenate with the previous buffer and call read again. - If the read buffer has a
'\n'
, we concatenate with the previous buffer up to'\n'
. - If we reach the end of the file (
read() == 0
), we concatenate with the previous buffer. - We finally point
line
to an allocated string that contains the entire line without the'\n'
. Then we release the memory allocated in the intermediate strings and return1
or0
for'\n'
andend_of_file
respectively. - If the parameters have any problems (
BUFFER_SIZE <= 0
), or if in any operation we were unable to allocate memory, we free whatever memory was allocated and return -1.
Tem essa documentacao bem legal do @harm-smits.
O get_next_line
lê uma linha inteira do arquivo indexado por fd
,
e faz o ponterio line
apontar para uma string allocada
com os conteudos dessa linha sem a quebra de linha '\n'
.
Nao temos que passar nada allocado para o get_next_line,
apenas passar o endereco de um ponteiro que vai apontar para a string allocada.
Precisamos poder dar free()
nessa string allocada depois da chamada.
A gente vai ler o arquivo com a funcao read()
,
que avanca a sua posicao no file descriptor automaticamente.
A gente precisa usar um ponteiro estatico como buffer do read para poder acessar o que foi lido pelo read nas chamadas anteriores.
Precisamos tratar as seguintes situacoes:
- Se o buffer lido nao tem
'\n'
, concatenamos com o buffer anterior e chamamos read novamente. - Se o buffer lido tem
'\n'
, concatenamos com o buffer anterior ate o'\n'
. - Se chegamos no final do arquivo (
read() = 0
), concatenamos com o buffer anterior. - Finalmente temos que apontar o ponteiro line passado
para uma string allocada que contenha a linha inteira sem o
'\n'
. Depois liberamos a memoria allocada nas strings intermediarias e retornamos 1 ou 0 para '\n' ou final do arquivo respectivamente. - Se os parametros tem algum problema (
BUFFER_SIZE <= 0
), ou em alguma dessas operacaoes nao conseguimos allocar memoria, liberamos toda a memoria allocada e retornamos -1.
All you need is a shell and a C compiler like gcc
or clang
.
To compile the entire thing just clone the repo and run make
with a positive BUFFER_SIZE
:
$ git clone https://github.com/librity/ft_get_next_line.git
$ cd ft_get_next_line
$ make d="'BUFFER_SIZE=42'"
This will generate a get_next_line.a
archive, which you can compile with
the example file:
$ gcc -g -D BUFFER_SIZE=42 -I ./includes ./examples/example.c get_next_line.a
$ ./a.out
Norminette Github Action by @AdrianWR
Part of the larger 42 Network, 42 São Paulo is a software engineering school that offers a healthy alternative to traditional education:
- It doesn't have any teachers and classes.
- Students learn by cooperating and correcting each other's work (peer-to-peer learning).
- Its focus is as much on social skills as it is on technical skills.
- It's completely free to anyone that passes its selection process - The Piscine
It's an amazing school, and I'm grateful for the opportunity.