1getline(3)                 Library Functions Manual                 getline(3)
2
3
4

NAME

6       getline, getdelim - delimited string input
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

50       On success, getline() and getdelim() return the  number  of  characters
51       read,  including  the delimiter character, but not including the termi‐
52       nating null byte ('\0').  This value can be  used  to  handle  embedded
53       null bytes in the line read.
54
55       Both  functions  return -1 on failure to read a line (including end-of-
56       file condition).  In the event of a failure, errno is set  to  indicate
57       the error.
58
59       If  *lineptr was set to NULL before the call, then the buffer should be
60       freed by the user program even on failure.
61

ERRORS

63       EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid).
64
65       ENOMEM Allocation or reallocation of the line buffer failed.
66

ATTRIBUTES

68       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
69       tributes(7).
70
71       ┌────────────────────────────────────────────┬───────────────┬─────────┐
72Interface                                   Attribute     Value   
73       ├────────────────────────────────────────────┼───────────────┼─────────┤
74getline(), getdelim()                       │ Thread safety │ MT-Safe │
75       └────────────────────────────────────────────┴───────────────┴─────────┘
76

STANDARDS

78       POSIX.1-2008.
79

HISTORY

81       GNU, POSIX.1-2008.
82

EXAMPLES

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

SEE ALSO

118       read(2), fgets(3), fopen(3), fread(3), scanf(3)
119
120
121
122Linux man-pages 6.04              2023-03-30                        getline(3)
Impressum