NAME
getdelim getline
— read a delimited record from stream
SYNOPSIS
ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
int delimiter, FILE *restrict stream);
ssize_t getline(char **restrict lineptr, size_t *restrict n,
FILE *restrict stream);
DESCRIPTION
The getdelim () function shall read from
stream until it encounters a character matching the
delimiter character. The delimiter
argument is an
int, the
value of which the application shall ensure is a character representable as
an
unsigned
char of equal value that terminates the read process. If the
delimiter argument has any other value, the behavior is
undefined.
The application shall ensure that *lineptr is a valid argument that could be passed to the free(3) function. If *n is non-zero, the application shall ensure that *lineptr either points to an object of size at least *n bytes, or is a null pointer.
If *lineptr is a null pointer or if the object pointed to by *lineptr is of insufficient size, an object shall be allocated as if by malloc(3) or the object shall be reallocated as if by realloc(3), respectively, such that the object is large enough to hold the characters to be written to it, including the terminating NUL, and *n shall be set to the new size. If the object was allocated, or if the reallocation operation moved the object, *lineptr shall be updated to point to the new object or new location. The characters read, including any delimiter, shall be stored in the object, and a terminating NUL added when the delimiter or end-of-file is encountered.
The getline () function shall be equivalent to
the getdelim () function with the
delimiter character equal to the <newline>
character.
The getdelim () and
getline () functions may mark the last data access
timestamp of the file associated with stream for update.
The last data access timestamp shall be marked for update by the first
successful execution of fgetc(3),
fgets(3), fread(3),
fscanf(3), getc(3),
getchar(3), getdelim (),
getline (), gets(3), or
scanf(3) using stream that returns data
not supplied by a prior call to ungetc(3).
RETURN VALUE
Upon successful completion, the getline () and
getdelim () functions shall return the number of
bytes written into the buffer, including the delimiter character if one was
encountered before EOF, but excluding the terminating NUL character. If the
end-of-file indicator for the stream is set, or if no characters were read
and the stream is at end-of-file, the end-of-file indicator for the stream
shall be set and the function shall return -1. If an error occurs, the error
indicator for the stream shall be set, and the function shall return -1 and
set
errno to
indicate the error.
ERRORS
For the conditions under which the
getdelim () and getline ()
functions shall fail and may fail, refer to fgetc(3).
In addition, these functions shall fail if:
These functions may fail if:
- [EOVERFLOW]
- The number of bytes to be written into the buffer, including the delimiter character (if encountered), would exceed {SSIZE_MAX}.
EXAMPLES
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/etc/motd", "r");
if (fp == NULL)
exit(1);
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
}
if (ferror(fp)) {
/* handle error */
}
free(line);
fclose(fp);
return 0;
}
APPLICATION USAGE
Setting *lineptr to a null pointer and *n to zero are allowed and a recommended way to start parsing a file.
The ferror(3) or feof(3) functions should be used to distinguish between an error condition and an end-of-file condition.
Although a NUL terminator is always supplied after the line, note that strlen (* lineptr) will be smaller than the return value if the line contains embedded NUL characters.
RATIONALE
These functions are widely used to solve the problem that the fgets(3) function has with long lines. The functions automatically enlarge the target buffers if needed. These are especially useful since they reduce code needed for applications.
FUTURE DIRECTIONS
None.
SEE ALSO
V2_chap02(3), fgetc(3), fgets(3), free(3), malloc(3), realloc(3)
XBD <stdio.h>
CHANGE HISTORY
First released in Issue 7.
POSIX.1-2008, Technical Corrigendum 1, XSH/TC1-2008/0237 [14] is applied.
POSIX.1-2008, Technical Corrigendum 2, XSH/TC2-2008/0153 [569], XSH/TC2-2008/0154 [571], and XSH/TC2-2008/0155 [570] are applied.