286 lines
6.6 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
getline, getdelim - delimited string input
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>></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 >= 200809L
|
|
<DT id="3">Before glibc 2.10:<DD>
|
|
_GNU_SOURCE
|
|
</DL>
|
|
</DL>
|
|
|
|
|
|
|
|
<A NAME="lbAD"> </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"> </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"> </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"> </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"> </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"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> FILE *stream;
|
|
<BR> char *line = NULL;
|
|
<BR> size_t len = 0;
|
|
<BR> ssize_t nread;
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s <file>\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> stream = fopen(argv[1], "r");
|
|
<BR> if (stream == NULL) {
|
|
<BR> perror("fopen");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> while ((nread = getline(&line, &len, stream)) != -1) {
|
|
<BR> printf("Retrieved line of length %zu:\n", nread);
|
|
<BR> fwrite(line, nread, 1, stdout);
|
|
<BR> }
|
|
<P>
|
|
<BR> free(line);
|
|
<BR> fclose(stream);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAJ"> </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"> </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"> </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>
|