PROLOG |NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |EXAMPLES |APPLICATION USAGE |RATIONALE |FUTURE DIRECTIONS |SEE ALSO |COPYRIGHT | |
GETDELIM(3P) POSIX Programmer's ManualGETDELIM(3P)This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
getdelim, getline — read a delimited record fromstream
#include <stdio.h> ssize_t getdelim(char **restrictlineptr, size_t *restrictn, intdelimiter, FILE *restrictstream); ssize_t getline(char **restrictlineptr, size_t *restrictn, FILE *restrictstream);
Thegetdelim() function shall read fromstream until it encounters a character matching thedelimiter character. Thedelimiter argument is anint, the value of which the application shall ensure is a character representable as anunsigned charof equal value that terminates the read process. If thedelimiter argument has any other value, the behavior is undefined. The application shall ensure that*lineptr is a valid argument that could be passed to thefree() 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 bymalloc() or the object shall be reallocated as if byrealloc(), 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. Thegetline() function shall be equivalent to thegetdelim() function with thedelimiter character equal to the <newline> character. Thegetdelim() andgetline() functions may mark the last data access timestamp of the file associated withstream for update. The last data access timestamp shall be marked for update by the first successful execution offgetc(),fgets(),fread(),fscanf(),getc(),getchar(),getdelim(),getline(),gets(), orscanf() usingstream that returns data not supplied by a prior call toungetc().
Upon successful completion, thegetline() andgetdelim() 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 seterrno to indicate the error.
For the conditions under which thegetdelim() andgetline() functions shall fail and may fail, refer tofgetc(3p). In addition, these functions shall fail if:EINVALlineptr orn is a null pointer.ENOMEMInsufficient memory is available. 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}.The following sections are informative. #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; }Setting*lineptr to a null pointer and*n to zero are allowed and a recommended way to start parsing a file. Theferror() orfeof() 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 thatstrlen(*lineptr) will be smaller than the return value if the line contains embedded NUL characters.
These functions are widely used to solve the problem that thefgets() 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.
None.
Section 2.5,Standard I/O Streams,fgetc(3p),fgets(3p),free(3p),malloc(3p),realloc(3p) The Base Definitions volume of POSIX.1‐2017,stdio.h(0p)
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online athttp://www.opengroup.org/unix/online.html . Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, seehttps://www.kernel.org/doc/man-pages/reporting_bugs.html .IEEE/The Open Group 2017GETDELIM(3P)Pages that refer to this page:stdio.h(0p), fgets(3p), getline(3p)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |