GETLINE(3) BSD Library Functions Manual GETLINE(3)NAME
getdelim, getline — get a line from a stream
Standard C Library (libc, -lc)
getdelim(char ** restrict linep, size_t * restrict linecapp,
int delimiter, FILE * restrict stream);
getline(char ** restrict linep, size_t * restrict linecapp,
FILE * restrict stream);
The getdelim() function reads a line from stream, delimited by the char‐
acter delimiter. The getline() function is equivalent to getdelim() with
the newline character as the delimiter. The delimiter character is
included as part of the line, unless the end of the file is reached. The
caller may provide a pointer to a malloc buffer for the line in *linep,
and the capacity of that buffer in *linecapp; if *linecapp is 0, then
*linep is treated as NULL. These functions may expand the buffer as
needed, as if via realloc(), and update *linep and *linecapp accordingly.
The getdelim() and getline() functions return the number of characters
written, excluding the terminating NUL character. The value -1 is
returned if an error occurs, or if end-of-file is reached.
The following code fragment reads lines from a file and writes them to
standard output. The fwrite() function is used in case the line contains
embedded NUL characters.
char *line = NULL;
size_t linecap = 0;
while ((linelen = getline(&line, &linecap, fp)) > 0)
fwrite(line, linelen, 1, stdout);
Many application writers used the name getline before the getline() func‐
tion was introduced in IEEE Std 1003.1 (“POSIX.1”), so a prototype is not
provided by default in order to avoid compatibility problems. Applica‐
tions that wish to use the getline() function described herein should
either request a strict IEEE Std 1003.1-2008 (“POSIX.1”) environment by
defining the macro _POSIX_C_SOURCE to the value 200809 or greater, or by
defining the macro _WITH_GETLINE, prior to the inclusion of <stdio.h>.
For compatibility with GNU libc, defining either _BSD_SOURCE or
_GNU_SOURCE prior to the inclusion of <stdio.h> will also make getline()
These functions may fail if:
[EINVAL] Either linep or linecapp is NULL.
[EOVERFLOW] No delimiter was found in the first SSIZE_MAX charac‐
These functions may also fail for any of the errors specified for fgets()
SEE ALSOfgetln(3), fgets(3), malloc(3)STANDARDS
The getdelim() and getline() functions conform to IEEE Std 1003.1-2008
These routines first appeared in FreeBSD 8.0.
There are no wide character versions of getdelim() or getline().
BSD March 29, 2009 BSD