getdelim(), getline()

get a line from a stream 

Function


SYNOPSIS

#define _WITH_GETLINE

#include <stdio.h>

ssize_t getdelim(char **linep, size_t *linecapp, int delimiter, FILE *stream);

ssize_t getline(char **linep, size_t *linecapp, FILE *stream);


DESCRIPTION

The getdelim() function reads a line from stream, delimited by the character delimiter. The getline() function is equivalent to getdelim() with the newline character as the delimiter. The delimiter character is included as part of the line, unless the end of the file is reached.

The caller may provide a pointer to a malloced buffer for the line in *linep, and the capacity of that buffer in *linecapp. These functions expand the buffer as needed, as if via realloc(). If linep points to a NULL pointer, a new buffer will be allocated. In either case, *linep and *linecapp will be updated accordingly.


RETURN VALUES

The getdelim() and getline() functions return the number of characters stored in the buffer, excluding the terminating NUL character. The value -1 is returned if an error occurs, or if end-of-file is reached.

These functions may fail if either linep or linecapp is NULL then errno is set to EINVAL, or no delimiter was found in teh first SSIZE_MAX characters in which case errno is set to EOVERFLOW.

These functions may also fail due to any of the errors specified for fgets() and malloc().


EXAMPLE

The following code fragment reads lines from a file and writes them to standard output. The fwrite() function is used in case the line contains embedded NUL characters.

   char *line = NULL;
   size_t linecap = 0;
   ssize_t linelen;
   while ((linelen = getline(&line, &linecap, fp)) > 0)
       fwrite(line, linelen, 1, stdout);
   free(line);

CONFORMANCE

The getdelim() and getline() functions conform to IEEE Std 1003.1-2008 'POSIX.1'


MULTITHREAD SAFETY LEVEL

MT-Safe.


PORTING ISSUES

Many application writers used the name getline before the getline() function was introduced in POSIX.1, so a prototype is not provided by default in order to avoid compatibility problems. Applications that wish to use the getline() function described herein should either request a strict IEEE Std 1003.1-2008 'POSIX.1' environment by defining the macro _POSIX_C_SOURCE to the value 200809 or greater, or by defining the macro _WITH_GETLINE, prior to the inclusion of <stdio.h>. For compatibility with GNU libc, defining either _BSD_SOURCE prior to the inclusion of <stdio.h> will also make getline() available.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
fgetln(), fgets(), malloc()


PTC MKS Toolkit 10.5 Documentation Build 40.