get_next_line disc

get_next_line

C project · file I/O, memory

Reads a file descriptor one line at a time with careful dynamic memory handling.

Open it in the console View the code on GitHub Resume (PDF) LinkedIn

The problem

Return one line at a time from a file descriptor, whatever the buffer size.

What I built

The hard part

Who owns the leftover buffer. The one static pointer is reassigned on every way out of the function: a failed read frees it and leaves it NULL, a last line without a newline empties it, and end of file frees it before returning NULL. So nothing stays allocated once a file is read to the end or a read fails: 0 bytes leaked at BUFFER_SIZE 1, 42 and 9999.

Built with

More projects