The C standard does not define such a function, but POSIX does.
The getline function described here (or by typing man getline if you are using a UNIX-like system) does what you ask.
It may not be available on systems other than POSIX (for example, on MS Windows).
A small program demonstrating its use:
#include <stdio.h> #include <stdlib.h> int main(void) { char *line = NULL; size_t n = 0; ssize_t result = getline(&line, &n, stdin); printf("result = %zd, n = %zu, line = \"%s\"\n", result, n, line); free(line); }
As with fgets , the character '\n' newline remains in the array.
source share