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 **restrict lineptr, size_t *restrict n,
12                       FILE *restrict stream);
13       ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
14                       int delim, FILE *restrict stream);
15
16   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18       getline(), getdelim():
19           Since glibc 2.10:
20               _POSIX_C_SOURCE >= 200809L
21           Before glibc 2.10:
22               _GNU_SOURCE
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 set to NULL before the call, then getline() will allo‐
30       cate a buffer for storing the line.  This buffer should be freed by the
31       user program even if getline() failed.
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 that a line delimiter other
42       than newline can be specified as the delimiter argument.  As with  get‐
43       line(),  a  delimiter  character is not added if one was not present in
44       the input 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 ('\0').  This value can be  used  to  handle  embedded
50       null bytes in the line read.
51
52       Both  functions  return -1 on failure to read a line (including end-of-
53       file condition).  In the event of a failure, errno is set  to  indicate
54       the error.
55

ERRORS

57       EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid).
58
59       ENOMEM Allocation or reallocation of the line buffer failed.
60

ATTRIBUTES

62       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
63       tributes(7).
64
65       ┌────────────────────────────────────────────┬───────────────┬─────────┐
66Interface                                   Attribute     Value   
67       ├────────────────────────────────────────────┼───────────────┼─────────┤
68getline(), getdelim()                       │ Thread safety │ MT-Safe │
69       └────────────────────────────────────────────┴───────────────┴─────────┘
70

CONFORMING TO

72       Both getline() and getdelim() were  originally  GNU  extensions.   They
73       were standardized in POSIX.1-2008.
74

EXAMPLES

76       #define _GNU_SOURCE
77       #include <stdio.h>
78       #include <stdlib.h>
79
80       int
81       main(int argc, char *argv[])
82       {
83           FILE *stream;
84           char *line = NULL;
85           size_t len = 0;
86           ssize_t nread;
87
88           if (argc != 2) {
89               fprintf(stderr, "Usage: %s <file>\n", argv[0]);
90               exit(EXIT_FAILURE);
91           }
92
93           stream = fopen(argv[1], "r");
94           if (stream == NULL) {
95               perror("fopen");
96               exit(EXIT_FAILURE);
97           }
98
99           while ((nread = getline(&line, &len, stream)) != -1) {
100               printf("Retrieved line of length %zd:\n", nread);
101               fwrite(line, nread, 1, stdout);
102           }
103
104           free(line);
105           fclose(stream);
106           exit(EXIT_SUCCESS);
107       }
108

SEE ALSO

110       read(2), fgets(3), fopen(3), fread(3), scanf(3)
111

COLOPHON

113       This  page  is  part of release 5.13 of the Linux man-pages project.  A
114       description of the project, information about reporting bugs,  and  the
115       latest     version     of     this    page,    can    be    found    at
116       https://www.kernel.org/doc/man-pages/.
117
118
119
120GNU                               2021-03-22                        GETLINE(3)
Impressum