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

NAME

6       getline, getdelim - delimited string input
7

SYNOPSIS

9       #define _GNU_SOURCE
10       #include <stdio.h>
11
12       ssize_t getline(char **lineptr, size_t *n, FILE *stream);
13
14       ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
15

DESCRIPTION

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

RETURN VALUE

39       On success, getline() and getdelim() return the  number  of  characters
40       read,  including  the delimiter character, but not including the termi‐
41       nating null byte.  This value can be used to handle embedded null bytes
42       in the line read.
43
44       Both  functions  return -1  on failure to read a line (including end of
45       file condition).
46

ERRORS

48       EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid).
49

CONFORMING TO

51       Both getline() and getdelim() are GNU extensions.  They  are  available
52       since libc 4.6.27.
53

EXAMPLE

55       #define _GNU_SOURCE
56       #include <stdio.h>
57       #include <stdlib.h>
58
59       int
60       main(void)
61       {
62           FILE * fp;
63           char * line = NULL;
64           size_t len = 0;
65           ssize_t read;
66
67           fp = fopen("/etc/motd", "r");
68           if (fp == NULL)
69               exit(EXIT_FAILURE);
70
71           while ((read = getline(&line, &len, fp)) != -1) {
72               printf("Retrieved line of length %zu :\n", read);
73               printf("%s", line);
74           }
75
76           if (line)
77               free(line);
78           exit(EXIT_SUCCESS);
79       }
80

SEE ALSO

82       read(2),   fgets(3),   fopen(3),   fread(3),  gets(3),  scanf(3),  fea‐
83       ture_test_macros(7)
84

COLOPHON

86       This page is part of release 3.22 of the Linux  man-pages  project.   A
87       description  of  the project, and information about reporting bugs, can
88       be found at http://www.kernel.org/doc/man-pages/.
89
90
91
92GNU                               2006-05-17                        GETLINE(3)
Impressum