1GETLINE(3) Linux Programmer's Manual GETLINE(3)
2
3
4
6 getline, getdelim - delimited string input
7
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 getline(), getdelim():
18 Since glibc 2.10:
19 _POSIX_C_SOURCE >= 200809L
20 Before glibc 2.10:
21 _GNU_SOURCE
22
24 getline() reads an entire line from stream, storing the address of the
25 buffer containing the text into *lineptr. The buffer is null-termi‐
26 nated and includes the newline character, if one was found.
27
28 If *lineptr is set to NULL and *n is set 0 before the call, then get‐
29 line() will allocate a buffer for storing the line. This buffer should
30 be freed by the user program even if getline() failed.
31
32 Alternatively, before calling getline(), *lineptr can contain a pointer
33 to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not
34 large enough to hold the line, getline() resizes it with realloc(3),
35 updating *lineptr and *n as necessary.
36
37 In either case, on a successful call, *lineptr and *n will be updated
38 to reflect the buffer address and allocated size respectively.
39
40 getdelim() works like getline(), except that a line delimiter other
41 than newline can be specified as the delimiter argument. As with get‐
42 line(), a delimiter character is not added if one was not present in
43 the input before end of file was reached.
44
46 On success, getline() and getdelim() return the number of characters
47 read, including the delimiter character, but not including the termi‐
48 nating null byte ('\0'). This value can be used to handle embedded
49 null bytes in the line read.
50
51 Both functions return -1 on failure to read a line (including end-of-
52 file condition). In the event of an error, errno is set to indicate
53 the cause.
54
56 EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid).
57
58 ENOMEM Allocation or reallocation of the line buffer failed.
59
61 For an explanation of the terms used in this section, see
62 attributes(7).
63
64 ┌──────────────────────┬───────────────┬─────────┐
65 │Interface │ Attribute │ Value │
66 ├──────────────────────┼───────────────┼─────────┤
67 │getline(), getdelim() │ Thread safety │ MT-Safe │
68 └──────────────────────┴───────────────┴─────────┘
69
71 Both getline() and getdelim() were originally GNU extensions. They
72 were standardized in POSIX.1-2008.
73
75 #define _GNU_SOURCE
76 #include <stdio.h>
77 #include <stdlib.h>
78
79 int
80 main(int argc, char *argv[])
81 {
82 FILE *stream;
83 char *line = NULL;
84 size_t len = 0;
85 ssize_t nread;
86
87 if (argc != 2) {
88 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
89 exit(EXIT_FAILURE);
90 }
91
92 stream = fopen(argv[1], "r");
93 if (stream == NULL) {
94 perror("fopen");
95 exit(EXIT_FAILURE);
96 }
97
98 while ((nread = getline(&line, &len, stream)) != -1) {
99 printf("Retrieved line of length %zu:\n", nread);
100 fwrite(line, nread, 1, stdout);
101 }
102
103 free(line);
104 fclose(stream);
105 exit(EXIT_SUCCESS);
106 }
107
109 read(2), fgets(3), fopen(3), fread(3), scanf(3)
110
112 This page is part of release 4.16 of the Linux man-pages project. A
113 description of the project, information about reporting bugs, and the
114 latest version of this page, can be found at
115 https://www.kernel.org/doc/man-pages/.
116
117
118
119GNU 2017-09-15 GETLINE(3)