1GETLINE(3)                 Linux Programmer's Manual                GETLINE(3)
2
3
4

NAME

6       getline, getdelim - delimited string input
7

SYNOPSIS

9       #include <stdio.h>
10
11       ssize_t getline(char **lineptr, size_t *n, FILE *stream);
12
13       ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
14
15   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17       Before glibc 2.10:
18       getline(), getdelim(): _GNU_SOURCE
19
20       Since glibc 2.10:
21       getline(),  getdelim():  _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >=
22       700
23

DESCRIPTION

25       getline() reads an entire line from stream, storing the address of  the
26       buffer  containing  the  text into *lineptr.  The buffer is null-termi‐
27       nated and includes the newline character, if one was found.
28
29       If *lineptr is NULL, then getline() will allocate a buffer for  storing
30       the  line,  which  should be freed by the user program.  (In this case,
31       the value in *n is ignored.)
32
33       Alternatively, before calling getline(), *lineptr can contain a pointer
34       to a malloc(3)-allocated buffer *n bytes in size.  If the buffer is not
35       large enough to hold the line, getline() resizes  it  with  realloc(3),
36       updating *lineptr and *n as necessary.
37
38       In  either  case, on a successful call, *lineptr and *n will be updated
39       to reflect the buffer address and allocated size respectively.
40
41       getdelim() works like getline(), except a  line  delimiter  other  than
42       newline can be specified as the delimiter argument.  As with getline(),
43       a delimiter character is not added if one was not present in the  input
44       before end of file was reached.
45

RETURN VALUE

47       On  success,  getline()  and getdelim() return the number of characters
48       read, including the delimiter character, but not including  the  termi‐
49       nating null byte.  This value can be used to handle embedded null bytes
50       in the line read.
51
52       Both functions return -1  on failure to read a line (including  end-of-
53       file condition).
54

ERRORS

56       EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid).
57

VERSIONS

59       These functions are available since libc 4.6.27.
60

CONFORMING TO

62       Both  getline()  and  getdelim()  were originally GNU extensions.  They
63       were standardized in POSIX.1-2008.
64

EXAMPLE

66       #define _GNU_SOURCE
67       #include <stdio.h>
68       #include <stdlib.h>
69
70       int
71       main(void)
72       {
73           FILE *fp;
74           char *line = NULL;
75           size_t len = 0;
76           ssize_t read;
77
78           fp = fopen("/etc/motd", "r");
79           if (fp == NULL)
80               exit(EXIT_FAILURE);
81
82           while ((read = getline(&line, &len, fp)) != -1) {
83               printf("Retrieved line of length %zu :\n", read);
84               printf("%s", line);
85           }
86
87           free(line);
88           exit(EXIT_SUCCESS);
89       }
90

SEE ALSO

92       read(2),  fgets(3),  fopen(3),  fread(3),   gets(3),   scanf(3),   fea‐
93       ture_test_macros(7)
94

COLOPHON

96       This  page  is  part of release 3.25 of the Linux man-pages project.  A
97       description of the project, and information about reporting  bugs,  can
98       be found at http://www.kernel.org/doc/man-pages/.
99
100
101
102GNU                               2010-06-12                        GETLINE(3)
Impressum