getline(3c) 맨 페이지 - 윈디하나의 솔라나라

개요

섹션
맨 페이지 이름
검색(S)

getline(3c)

Standard C Library Functions                                       getline(3C)



NAME
       getline, getdelim - delimited string input

SYNOPSIS
       #include <stdio.h>

       ssize_t getline(char **restrict lineptr, size_t *restrict n,
       FILE *restrict stream);


       ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
       int delimiter, FILE *restrict stream);

DESCRIPTION
       The  getline()  function  reads an entire line from stream, storing the
       address of the buffer containing the line in *lineptr.  The  buffer  is
       null-terminated and includes the NEWLINE character if one was found.


       If *lineptr is a null pointer, getline() allocates a buffer for storing
       the line. Alternatively, before the call  to  getline(),  *lineptr  can
       contain  a pointer to a buffer allocated by malloc(3C) whose size is *n
       bytes. If the buffer is not large enough to store the  line,  getline()
       resizes  the buffer with realloc(3C). In either case, a successful call
       to getline() updates *lineptr and *n to reflect the buffer address  and
       size, respectively. The buffer should be freed with a call to free(3C).


       The getdelim() function is identical to getline(), except a line delim‐
       iter other than NEWLINE can be specified as the delimiter argument.  As
       with  getline(),  a  delimiter  character  is  not added if one was not
       present in stream before end-of-file was reached.

RETURN VALUES
       Upon successful completion,  the  getline()  and  getdelim()  functions
       return  the number of characters written into the buffer, including the
       delimiter character but excluding the terminating null character.  Upon
       failure  to  read a line (including end of file condition), these func‐
       tion return −1 and set errno to indicate the error.

ERRORS
       The getline() and getdelim() functions will fail if:

       EINVAL    Either lineptr or n is a null pointer.


       ENOMEM    Insufficient memory is available.



       The getline() and getdelim() functions may fail if:

       EOVERFLOW    More than {SSIZE_MAX} characters were read without encoun‐
                    tering the delimiter character.



       See fgetc(3C) for other conditions under which these functions will and
       may fail.

EXAMPLES
       Example 1 Retrieve a line length.


         #include <stdio.h>
         #include <stdlib.h>

         int main(void)
         {
             FILE *fp;
             char *line = NULL;
             size_t len = 0;
             ssize_t read;
             fp = fopen("/etc/motd", "r");
             if (fp == NULL)
                 exit(1);
             while ((read = getline(&line, &len, fp)) != -1) {
                 printf("Retrieved line of length %zu :\n", read);
                 printf("%s", line);
             }
             if (ferror(fp)) {
                 /* handle error */
             }
             free(line);
             fclose(fp);
             return 0;
         }



ATTRIBUTES
       See attributes(7) for descriptions of the following attributes:


       tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) ATTRIBUTE  TYPEAT‐
       TRIBUTE VALUE _ Interface StabilityCommitted _ MT-LevelMT-Safe


SEE ALSO
       fgetc(3C), fgets(3C), free(3C), malloc(3C), realloc(3C), attributes(7)



Oracle Solaris 11.4               11 Oct 2010                      getline(3C)
맨 페이지 내용의 저작권은 맨 페이지 작성자에게 있습니다.
RSS ATOM XHTML 5 CSS3