NAME
getline, getdelim - delimited string input
SYNOPSIS
#include <stdio.h> ssize_t getline(char **lineptr, size_t *n, FILE *stream); ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *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
getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null- terminated and includes the newline character, if one was found. If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed. Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary. In either case, on a successful call, *lineptr and *n will be updated to reflect the buffer address and allocated size respectively. getdelim() works like getline(), except that a line delimiter other than newline can be specified as the delimiter argument. As with getline(), a delimiter character is not added if one was not 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'). This value can be used to handle embedded null bytes in the line read. Both functions return -1 on failure to read a line (including end-of- file condition). In the event of an error, errno is set to indicate the cause.
ERRORS
EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid).
ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). ┌──────────────────────┬───────────────┬─────────┐ │Interface │ Attribute │ Value │ ├──────────────────────┼───────────────┼─────────┤ │getline(), getdelim() │ Thread safety │ MT-Safe │ └──────────────────────┴───────────────┴─────────┘
CONFORMING TO
Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008.
EXAMPLE
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> int main(void) { FILE *stream; char *line = NULL; size_t len = 0; ssize_t read; stream = fopen("/etc/motd", "r"); if (stream == NULL) exit(EXIT_FAILURE); while ((read = getline(&line, &len, stream)) != -1) { printf("Retrieved line of length %zu :\n", read); printf("%s", line); } free(line); fclose(stream); exit(EXIT_SUCCESS); }
SEE ALSO
read(2), fgets(3), fopen(3), fread(3), scanf(3)
COLOPHON
This page is part of release 4.09 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.
More Linux Commands
manpages/Tcl_IsSafe.3.html
Tcl_IsSafe(3) - manage multiple Tcl interpreters, aliases an
These procedures are intended for access to the multiple interpreter facility from inside C programs. They enable managing multiple interpreters in a hierarchic
manpages/SDL_BuildAudioCVT.3.html
SDL_BuildAudioCVT(3) - Initializes a SDL_AudioCVT structure
Before an SDL_AudioCVT structure can be used to convert audio data it must be initialized with source and destination information. src_format and dst_format are
manpages/glutReportErrors.3.html
glutReportErrors(3) - for debugging purposes; prints out Ope
This routine prints out any OpenGL run-time errors pending and clears the errors. This routine typically should only be used for debugging purposes since callin
manpages/dn_comp.3.html
dn_comp(3) - resolver routines (Library - Linux man page)...
These functions make queries to and interpret the responses from Internet domain name servers. The res_init() function reads the configuration files (see resolv
manpages/Tk_MinReqHeight.3.html
Tk_MinReqHeight(3) - retrieve information from Tk's local da
Tk_WindowId and the other names listed above are all macros that return fields from Tks local data structure for tkwin. None of these macros requires any intera
manpages/dhclient.conf.5.html
dhclient.conf(5) - DHCP client configuration file (ManPage)
The dhclient.conf file contains configuration information for dhclient, the Internet Systems Consortium DHCP Client. The dhclient.conf file is a free-form ASCII
manpages/gamma.3.html
gamma(3) - (logarithm of the) gamma function (Man Page).....
These functions are deprecated: instead, use either the tgamma(3) or the lgamma(3) functions, as appropriate. For the definition of the Gamma function, see tgam
manpages/systemd.journal-fields.7.html
systemd.journal-fields(7) Special journal fields (Man Page)
Entries in the journal resemble an environment block in their syntax but with fields that can include binary data. Primarily, fields are formatted UTF-8 text st
manpages/Mail::SPF::v1::Record.3pm.html
Mail::SPF::v1::Record(3pm) - SPFv1 record class (Man Page)
An object of class Mail::SPF::v1::Record represents an SPFv1 (v=spf1) record. Constructors The following constructors are provided: new(%options): returns Mail:
manpages/lzdiff.1.html
lzdiff(1) - compare compressed files - Linux manual page....
xzcmp and xzdiff invoke cmp(1) or diff(1) on files compressed with xz(1), lzma(1), gzip(1), or bzip2(1). All options specified are passed directly to cmp(1) or
manpages/tkpppoe.1.html
tkpppoe(1) Graphical interface for controlling rp-pppoe.....
tkpppoe is a graphical program for controlling PPPoE links. It works with the RP-PPPoE package and has its own HTML manual. FILES /etc/ppp/rp-pppoe-gui/connecti
manpages/lzfgrep.1.html
lzfgrep(1) - search compressed files for a regular expressio
xzgrep invokes grep(1) on files which may be either uncompressed or compressed with xz(1), lzma(1), gzip(1), or bzip2(1). All options specified are passed direc
