getline(3)
โจ getline(3) โจ
okay bestie, welcome to the library functions manual, we're literally about to read strings off a stream together ๐ this is the getline(3) page, and yes it's giving "delimited string input" energy.
๐ NAME ๐
getline, getdelim โ delimited string input
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ธ SYNOPSIS ๐ธ
#include <stdio.h>
ssize_t getline(char **restrict lineptr, size_t *restrict n,
FILE *restrict stream);
ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
int delim, FILE *restrict stream);
๐ฆ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): ๐ฆ
getline(), getdelim():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
๐ DESCRIPTION ๐
so getline() reads an entire line from stream, and it stashes the address of the buffer holding that text into *lineptr. the buffer is null-terminated, and bestie, it includes the newline character if one was found. periodt.
if *lineptr is set to NULL before the call, then getline() will allocate a buffer to store the line. and heads up โ that buffer should be freed by the user program even if getline() failed. yeah, even then. ๐ญ
alternatively, before calling getline(), *lineptr can already contain a pointer to a malloc(3)-allocated buffer that's *n bytes in size. if that buffer isn't big enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary. it will literally adapt. main character behavior.
either way, on a successful call, *lineptr and *n get updated to reflect the buffer address and the allocated size respectively.
getdelim() works like getline(), except that you can specify a line delimiter other than newline as the delimiter argument. and same as with getline(): a delimiter character is not added if one wasn't present in the input before end of file was reached.
๐ฅ RETURN VALUE ๐ฅ
on success, getline() and getdelim() return the number of characters read, including the delimiter character, but not including the terminating null byte ('\0'). and the slay here? that value can be used to handle embedded null bytes in the line read ๐
at end of file, both functions return -1 with the file stream end-of-file indicator set. on error, both functions return -1 with the file stream error indicator set, and errno is set to indicate the error.
if *lineptr was set to NULL before the call, then the buffer should be freed by the user program even on failure. not me repeating myself but this one matters fr fr.
๐ ERRORS ๐
EINVALโ bad arguments (norlineptrisNULL, or stream is not valid).ENOMEMโ allocation or reallocation of the line buffer failed.
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, see attributes(7).
getline(),getdelim()โ Thread safety โMT-Safe๐ซถ
โญ STANDARDS โญ
POSIX.1-2008.
๐ง HISTORY ๐ง
GNU, POSIX.1-2008.
๐ EXAMPLES ๐
here's the whole vibe, copy it exactly bestie:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
FILE *stream;
char *line = NULL;
size_t size = 0;
ssize_t nread;
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
exit(EXIT_FAILURE);
}
stream = fopen(argv[1], "r");
if (stream == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
while ((nread = getline(&line, &size, stream)) != -1) {
printf("Retrieved line of length %zd:\n", nread);
fwrite(line, nread, 1, stdout);
}
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
}
๐ SEE ALSO ๐
read(2), fgets(3), fopen(3), fread(3), scanf(3)
and that's the whole page, bestie!! this was Linux man-pages 6.20, dated 2026-02-08 โ literally iconic, no cap. now go read some lines ๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2001, John Levon <moz@compsoc.man.ac.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms