{"id":5177,"date":"2022-12-20T18:37:11","date_gmt":"2022-12-20T21:37:11","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/getdelim-man3p\/"},"modified":"2022-12-20T18:37:11","modified_gmt":"2022-12-20T21:37:11","slug":"getdelim-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/getdelim-man3p\/","title":{"rendered":"GETDELIM (man3p)"},"content":{"rendered":"<h1 align=\"center\">GETDELIM<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s 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.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">getdelim, getline \u2014 read a delimited record from <i>stream<\/i><\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <stdio.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">ssize_t getdelim(char **restrict <i>lineptr<\/i>, size_t *restrict <i>n<\/i>, <br \/> int <i>delimiter<\/i>, FILE *restrict <i>stream<\/i>); <br \/> ssize_t getline(char **restrict <i>lineptr<\/i>, size_t *restrict <i>n<\/i>, <br \/> FILE *restrict <i>stream<\/i>);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getdelim<\/i>() function shall read from <i>stream<\/i> until it encounters a character matching the <i>delimiter<\/i> character. The <i>delimiter<\/i> argument is an <b>int<\/b>, the value of which the application shall ensure is a character representable as an <b>unsigned char<\/b> of equal value that terminates the read process. If the <i>delimiter<\/i> argument has any other value, the behavior is undefined.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The application shall ensure that <i>*lineptr<\/i> is a valid argument that could be passed to the <i>free<\/i>() function. If <i>*n<\/i> is non-zero, the application shall ensure that <i>*lineptr<\/i> either points to an object of size at least <i>*n<\/i> bytes, or is a null pointer.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>*lineptr<\/i> is a null pointer or if the object pointed to by <i>*lineptr<\/i> is of insufficient size, an object shall be allocated as if by <i>malloc<\/i>() or the object shall be reallocated as if by <i>realloc<\/i>(), respectively, such that the object is large enough to hold the characters to be written to it, including the terminating NUL, and <i>*n<\/i> shall be set to the new size. If the object was allocated, or if the reallocation operation moved the object, <i>*lineptr<\/i> 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getline<\/i>() function shall be equivalent to the <i>getdelim<\/i>() function with the <i>delimiter<\/i> character equal to the <newline> character.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>getdelim<\/i>() and <i>getline<\/i>() functions may mark the last data access timestamp of the file associated with <i>stream<\/i> for update. The last data access timestamp shall be marked for update by the first successful execution of <i>fgetc<\/i>(), <i>fgets<\/i>(), <i>fread<\/i>(), <i>fscanf<\/i>(), <i>getc<\/i>(), <i>getchar<\/i>(), <i>getdelim<\/i>(), <i>getline<\/i>(), <i>gets<\/i>(), or <i>scanf<\/i>() using <i>stream<\/i> that returns data not supplied by a prior call to <i>ungetc<\/i>().<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, the <i>getline<\/i>() and <i>getdelim<\/i>() 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 \u22121. If an error occurs, the error indicator for the stream shall be set, and the function shall return \u22121 and set <i>errno<\/i> to indicate the error.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For the conditions under which the <i>getdelim<\/i>() and <i>getline<\/i>() functions shall fail and may fail, refer to <i>fgetc<\/i>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">In addition, these functions shall fail if:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"50%\">\n<p><i>lineptr<\/i> or <i>n<\/i> is a null pointer.<\/p>\n<\/td>\n<td width=\"28%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOMEM<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"50%\">\n<p>Insufficient memory is available.<\/p>\n<\/td>\n<td width=\"28%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions may fail if: <b><br \/> EOVERFLOW<\/b><\/p>\n<p style=\"margin-left:22%;\">The number of bytes to be written into the buffer, including the delimiter character (if encountered), would exceed {SSIZE_MAX}.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <stdio.h> <br \/> #include <stdlib.h><\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">int main(void) <br \/> { <br \/> FILE *fp; <br \/> char *line = NULL; <br \/> size_t len = 0; <br \/> ssize_t read; <br \/> fp = fopen(&#8220;\/etc\/motd&#8221;, &#8220;r&#8221;); <br \/> if (fp == NULL) <br \/> exit(1); <br \/> while ((read = getline(&#038;line, &#038;len, fp)) != -1) { <br \/> printf(&#8220;Retrieved line of length %zu :n&#8221;, read); <br \/> printf(&#8220;%s&#8221;, line); <br \/> } <br \/> if (ferror(fp)) { <br \/> \/bin \/boot \/dead.letter \/dev \/etc \/home \/initrd \/lib \/lib64 \/lost+found \/media \/mnt \/opt \/proc \/release-notes.html \/release-notes.txt \/root \/run \/sbin \/srv \/sys \/tmp \/usr \/var handle error bodies\/ usr\/ <br \/> } <br \/> free(line); <br \/> fclose(fp); <br \/> return 0; <br \/> }<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Setting <i>*lineptr<\/i> to a null pointer and <i>*n<\/i> to zero are allowed and a recommended way to start parsing a file.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>ferror<\/i>() or <i>feof<\/i>() functions should be used to distinguish between an error condition and an end-of-file condition.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Although a NUL terminator is always supplied after the line, note that <i>strlen<\/i>(*<i>lineptr<\/i>) will be smaller than the return value if the line contains embedded NUL characters.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">These functions are widely used to solve the problem that the <i>fgets<\/i>() 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.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>Section 2.5<\/i>, <i>Standard I\/O Streams<\/i>, <i>fgetc<\/i>(), <i>fgets<\/i>(), <i>free<\/i>(), <i>malloc<\/i>(), <i>realloc<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><stdio.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; 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 at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  getdelim, getline \u2014 read a delimited record from stream <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[2067,1594],"class_list":["post-5177","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-getdelim","tag-man3p"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=5177"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/5177\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=5177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=5177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=5177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}