man-pages/man3/getline.3.html
2021-03-31 01:06:50 +01:00

286 lines
6.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of GETLINE</TITLE>
</HEAD><BODY>
<H1>GETLINE</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
getline, getdelim - delimited string input
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</B>
<B>ssize_t getline(char **</B><I>lineptr</I><B>, size_t *</B><I>n</I><B>, FILE *</B><I>stream</I><B>);</B>
<B>ssize_t getdelim(char **</B><I>lineptr</I><B>, size_t *</B><I>n</I><B>, int </B><I>delim</I><B>, FILE *</B><I>stream</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>getline</B>(),
<B>getdelim</B>():
<DL COMPACT><DT id="1"><DD>
<DL COMPACT>
<DT id="2">Since glibc 2.10:<DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200809L
<DT id="3">Before glibc 2.10:<DD>
_GNU_SOURCE
</DL>
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<B>getline</B>()
reads an entire line from <I>stream</I>,
storing the address of the buffer containing the text into
<I>*lineptr</I>.
The buffer is null-terminated and includes the newline character, if
one was found.
<P>
If
<I>*lineptr</I>
is set to NULL and
<I>*n</I>
is set 0 before the call, then
<B>getline</B>()
will allocate a buffer for storing the line.
This buffer should be freed by the user program
even if
<B>getline</B>()
failed.
<P>
Alternatively, before calling
<B>getline</B>(),
<I>*lineptr</I>
can contain a pointer to a
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)-allocated
buffer
<I>*n</I>
bytes in size.
If the buffer is not large enough to hold the line,
<B>getline</B>()
resizes it with
<B><A HREF="/cgi-bin/man/man2html?3+realloc">realloc</A></B>(3),
updating
<I>*lineptr</I>
and
<I>*n</I>
as necessary.
<P>
In either case, on a successful call,
<I>*lineptr</I>
and
<I>*n</I>
will be updated to reflect the buffer address and allocated size respectively.
<P>
<B>getdelim</B>()
works like
<B>getline</B>(),
except that a line delimiter other than newline can be specified as the
<I>delimiter</I>
argument.
As with
<B>getline</B>(),
a delimiter character is not added if one was not present
in the input before end of file was reached.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>getline</B>()
and
<B>getdelim</B>()
return the number of characters read, including the delimiter character,
but not including the terminating null byte ('\0').
This value can be used
to handle embedded null bytes in the line read.
<P>
Both functions return -1 on failure to read a line (including end-of-file
condition).
In the event of an error,
<I>errno</I>
is set to indicate the cause.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="4"><B>EINVAL</B>
<DD>
Bad arguments
(<I>n</I>
or
<I>lineptr</I>
is NULL, or
<I>stream</I>
is not valid).
<DT id="5"><B>ENOMEM</B>
<DD>
Allocation or reallocation of the line buffer failed.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>getline</B>(),
<B>getdelim</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
Both
<B>getline</B>()
and
<B>getdelim</B>()
were originally GNU extensions.
They were standardized in POSIX.1-2008.
<A NAME="lbAI">&nbsp;</A>
<H2>EXAMPLE</H2>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;*stream;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*line&nbsp;=&nbsp;NULL;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;len&nbsp;=&nbsp;0;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;nread;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;file&gt;\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;stream&nbsp;=&nbsp;fopen(argv[1],&nbsp;&quot;r&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(stream&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;fopen&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((nread&nbsp;=&nbsp;getline(&amp;line,&nbsp;&amp;len,&nbsp;stream))&nbsp;!=&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Retrieved&nbsp;line&nbsp;of&nbsp;length&nbsp;%zu:\n&quot;,&nbsp;nread);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fwrite(line,&nbsp;nread,&nbsp;1,&nbsp;stdout);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(line);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;fclose(stream);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+read">read</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+fgets">fgets</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fopen">fopen</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fread">fread</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+scanf">scanf</A></B>(3)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="6"><A HREF="#lbAB">NAME</A><DD>
<DT id="7"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="8"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="9"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="10"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="11"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAI">EXAMPLE</A><DD>
<DT id="14"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:44 GMT, March 31, 2021
</BODY>
</HTML>