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 || _XOPEN_SOURCE >= 700
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 NULL, then getline() will allocate a buffer for storing
29 the line, which should be freed by the user program. (In this case,
30 the value in *n is ignored.)
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
59 These functions are available since libc 4.6.27.
60
62 Both getline() and getdelim() were originally GNU extensions. They
63 were standardized in POSIX.1-2008.
64
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
92 read(2), fgets(3), fopen(3), fread(3), gets(3), scanf(3)
93
95 This page is part of release 3.53 of the Linux man-pages project. A
96 description of the project, and information about reporting bugs, can
97 be found at http://www.kernel.org/doc/man-pages/.
98
99
100
101GNU 2013-04-19 GETLINE(3)