getline(3) Library Functions Manual getline(3) getline, getdelim - C (libc, -lc) #include ssize_t getline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream); ssize_t getdelim(char **restrict lineptr, size_t *restrict n, int delim, FILE *restrict stream); glibc (. feature_test_macros(7)): getline(), getdelim(): glibc 2.10: _POSIX_C_SOURCE >= 200809L glibc 2.10: _BSD_SOURCE getline() stream, *lineptr. null , . If *lineptr is set to NULL before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed. , getline(), *lineptr , malloc(3) *n . , getline() realloc(3), *lineptr *n . *lineptr *n , . getdelim() getline(), , delimiter , . getline(), - , . On success, getline() and getdelim() 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. Both functions return -1 on failure to read a line (including end-of-file condition). In the event of a failure, errno is set to indicate the error. If *lineptr was set to NULL before the call, then the buffer should be freed by the user program even on failure. EINVAL (n lineptr NULL stream). ENOMEM . attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getline(), getdelim() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ POSIX.1-2008. GNU, POSIX.1-2008. #define _GNU_SOURCE #include #include int main(int argc, char *argv[]) { FILE *stream; char *line = NULL; size_t len = 0; ssize_t nread; if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } stream = fopen(argv[1], "r"); if (stream == NULL) { perror("fopen"); exit(EXIT_FAILURE); } while ((nread = getline(&line, &len, stream)) != -1) { printf("Retrieved line of length %zd:\n", nread); fwrite(line, nread, 1, stdout); } free(line); fclose(stream); exit(EXIT_SUCCESS); } read(2), fgets(3), fopen(3), fread(3), scanf(3) () Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 15 2024 . getline(3)