getlineのヘルプ・マニュアル
日本語 英語
getline --help
man getline
GETLINE(3) Linux Programmer’s Manual GETLINE(3)
名前
getline, getdelim - 区切り文字までの文字列入力を読み込む
書式
#define _GNU_SOURCE
#include
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
説明
getline() は stream から 1 行全てを読み込み、テキストが含まれているバッ
ファのアドレスを *lineptr に格納する。バッファはヌル文字 (\0) で終端 さ
れる。改行文字が見つかった場合は、改行文字もバッファに格納される。
*lineptr が NULL の場合、 getline() は行の内容を格納するためのバッファ
を確保する。このバッファはユーザーのプログラムで解放すべきである。 (*n
の値は無視される。)
別 の方法として、 getline() を呼び出す際に、 *lineptr に malloc(3) で確
保した大きさ *n バイトのバッファへのポインタを入れて渡すこともできる 。
読み込んだ行を保持するのに十分なバッファがない場合、 getline() は real-
loc(3) を使ってバッファのサイズを変更し、必要に応じて *lineptr と *n を
更新する。
ど ちらの場合でも、呼び出しに成功したときには、 *lineptr と *n をバッフ
ァのアドレスと割り当てたサイズを反映した値に更新する。
getdelim() は getline() と同じように動作するが、改行文字以外の区切り 文
字 を引き数 delim に指定することができる。 getline() と同様に、ファイル
終端に達するまでに入力行に区切り文字が見付からない場合は、区切り文字 を
バッファに追加しない。
返り値
成功した場合、 getline() と getdelim() は読み込んだ文字数を返す。文字数
には区切り文字を含むが、終端に使う NULL バイトは含まない。この値によ っ
て、読み込んだ行に含まれる NULL バイトを操作することができる。
ど ちらの関数も、行の読み込みに失敗した場合には -1 を返す (ファイルの終
端に達した場合にも -1 を返す)。
エラー
EINVAL 引き数が不正である (n または lineptr が NULL である。も し く は
stream が有効でない)。
準拠
getline() と getdelim() は、ともに GNU による拡張である。これらの関数は
libc 4.6.27 以降で使用可能である。
例
#define _GNU_SOURCE
#include
#include
int
main(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/etc/motd", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
}
if (line)
free(line);
exit(EXIT_SUCCESS);
}
関連項目
read(2), fgets(3), fopen(3), fread(3), gets(3), scanf(3), fea-
ture_test_macros(7)
GNU 2006-05-17 GETLINE(3)
GETLINE(3) Linux Programmer’s Manual GETLINE(3)
NAME
getline, getdelim - delimited string input
SYNOPSIS
#define _GNU_SOURCE
#include
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-termi-
nated 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. (The value in *n
is ignored.)
Alternatively, before calling getline(), *lineptr can contain a pointer
to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not
large enough to hold the line, getline() resizes it with realloc(3),
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.
RETURN VALUE
On success, getline() and getdelim() return the number of characters
read, including the delimiter character, but not including the termi-
nating null byte. 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).
ERRORS
EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid).
CONFORMING TO
Both getline() and getdelim() are GNU extensions. They are available
since libc 4.6.27.
EXAMPLE
#define _GNU_SOURCE
#include
#include
int
main(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/etc/motd", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
printf("Retrieved line of length %zu :\n", read);
printf("%s", line);
}
if (line)
free(line);
exit(EXIT_SUCCESS);
}
SEE ALSO
read(2), fgets(3), fopen(3), fread(3), gets(3), scanf(3), fea-
ture_test_macros(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2006-05-17 GETLINE(3)