'getline()'에 해당되는 글 1건

  1. 2009.06.25 getline()
Linux API/network2009. 6. 25. 19:40
말그대로 라인을 얻어오는 함수이다.

SYNOPSIS
       #define _GNU_SOURCE
       #include <stdio.h>

       ssize_t getline(char **lineptr, size_t *n, FILE *stream);
       ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);

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

       If *lineptr is NULL, then getline() will allocate a buffer for storing the line, which should  be  freed  by  the
       user  program.   Alternatively,  before calling getline(), *lineptr can contain a pointer to a malloc()-allocated
       buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(),
       updating  *lineptr  and *n as necessary. In either case, on a successful call, *lineptr and *n will be updated to
       reflect the buffer address and allocated size respectively.

       getdelim() works like getline(), except a line delimiter 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 the input before end of
       file was reached.

함수형을 보면, 파일에서 한줄씩(그러니까 CR/LF 까지) 읽어 오는데,
주의해야 할 점은, getline() 함수 내부적으로 malloc()를 호출 한다는 것이다.
즉, 만약에 malloc()을 해주지 않으려면, NULL 값인 포인터를 넘겨주면
알아서 malloc() 하거나 realloc() 해주므로 많은 신경을 쓰지 않아도 된다.

단, 마지막 getline() 호출 후, 사용이 끝나면 반드시 free()를 해주어야 한다.

[링크 : http://linux.die.net/man/3/getline]
Posted by 구차니