getline, getwline, getdelim, getwdelim
<stdio.h>
wint_t delimiter, FILE * stream);
stream as if by fgetc , until delimiter is encountered, storing the characters in the buffer of size *n pointed to by *lineptr, automatically increasing its size as if by realloc to fit the entire input, including the delimiter, and adding a null terminator. The pointer returned by realloc is written back into *lineptr. *lineptr may be null, in which case previous value of *n is ignored and getline allocates a new buffer as if by malloc . In any case the final length of the allocated buffer is written to *n. The behavior is undefined if delimiter has a value that is outside the range of unsigned char or EOF .delimiter must be a valid wchar_t or WEOF.If *lineptr is not null, the behavior is undefined if *lineptr is not a pointer that can be passed to free or if *n is greater than the size of the allocated memory pointed to by *lineptr. The behavior is also undefined if n is not a valid pointer suitable for writing a value of type size_t .
As all functions from Dynamic Memory TR, getline is only guaranteed to be available if __STDC_ALLOC_LIB__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT2__ to the integer constant 1 before including stdio.h.
Contents
[edit] Parameters
[edit] Return value
The number of characters stored in the buffer, including the delimiter, but excluding the null terminator.
On error, returns -1 and sets feof or ferror on stream.
[edit] Notes
These functions are identical to their POSIX versions except that it is allowed, but not required to set errno on error.
[edit] Example
#ifdef __STDC_ALLOC_LIB__ #define __STDC_WANT_LIB_EXT2__ 1 #else #define _POSIX_C_SOURCE 200809L #endif #include <stdio.h> #include <stdlib.h> void get_y_or_n(void) { char *response = NULL ; size_t len; printf ("Continue? [y] n: "); if((getline(&response, &len, stdin ) < 0) || (len && response[0] == 'n')) { free (response); exit (0); } free (response); return; } int main(void) { get_y_or_n(); }
Output:
Continue? [y] n: