.\" Copyright (c) 2001 John Levon .\" Based in part on GNU libc documentation .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" License. .\" .\" Japanese Version Copyright (c) 2001 Yuichi SATO .\" all rights reserved. .\" Translated 2001-11-09, Yuichi SATO .\" Updated 2006-07-20, Akihiro MOTOKI , LDP v2.34 .\" Updated 2010-04-18, Akihiro MOTOKI, LDP v3.24 .\" .\"WORD: delimiter 区切り文字 .\" .TH GETLINE 3 2010-06-12 "GNU" "Linux Programmer's Manual" .\"O .SH NAME .SH 名前 .\"O getline, getdelim \- delimited string input getline, getdelim \- 区切り文字までの文字列入力を読み込む .\"O .SH SYNOPSIS .SH 書式 .nf .B #include .sp .BI "ssize_t getline(char **" lineptr ", size_t *" n ", FILE *" stream ); .BI "ssize_t getdelim(char **" lineptr ", size_t *" n ", int " delim \ ", FILE *" stream ); .fi .sp .in -4n .\"O Feature Test Macro Requirements for glibc (see .\"O .BR feature_test_macros (7)): glibc 向けの機能検査マクロの要件 .RB ( feature_test_macros (7) 参照): .in .sp .ad l .BR getline (), .BR getdelim (): .PD 0 .RS 4 .TP 4 .\"O Since glibc 2.10: glibc 2.10 以降: _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700 .TP .\"O Before glibc 2.10: glibc 2.10 より前: _GNU_SOURCE .RE .PD .ad .\"O .SH DESCRIPTION .SH 説明 .\"O .BR getline () .\"O reads an entire line from \fIstream\fP, .\"O storing the address of the buffer containing the text into .\"O .IR "*lineptr" . .BR getline () は \fIstream\fP から 1 行全てを読み込み、テキストが含まれているバッファのアドレスを .I "*lineptr" に格納する。 .\"O The buffer is null-terminated and includes the newline character, if .\"O one was found. バッファはヌル文字 (\e0) で終端される。 改行文字が見つかった場合は、改行文字もバッファに格納される。 .\"O If .\"O .I "*lineptr" .\"O is NULL, then .\"O .BR getline () .\"O will allocate a buffer for storing the line, .\"O which should be freed by the user program. .\"O (In this case, the value in .\"O .I *n .\"O is ignored.) .I "*lineptr" が NULL の場合、 .BR getline () は行の内容を格納するためのバッファを確保する。 このバッファはユーザーのプログラムで解放すべきである (この場合、 .I *n の値は無視される)。 .\"O Alternatively, before calling .\"O .BR getline (), .\"O .I "*lineptr" .\"O can contain a pointer to a .\"O .BR malloc (3)\-allocated .\"O buffer .\"O .I "*n" .\"O bytes in size. 別の方法として、 .BR getline () を呼び出す際に、 .I "*lineptr" に .BR malloc (3) で確保した大きさ .I "*n" バイトのバッファへのポインタを入れて渡すこともできる。 .\"O If the buffer is not large enough to hold the line, .\"O .BR getline () .\"O resizes it with .\"O .BR "realloc(3)" , .\"O updating .\"O .I "*lineptr" .\"O and .\"O .I "*n" .\"O as necessary. 読み込んだ行を保持するのに十分なバッファがない場合、 .BR getline () は .BR realloc (3) を使ってバッファのサイズを変更し、必要に応じて .I "*lineptr" と .I "*n" を更新する。 .\"O In either case, on a successful call, .\"O .I "*lineptr" .\"O and .\"O .I "*n" .\"O will be updated to reflect the buffer address and allocated size respectively. どちらの場合でも、呼び出しに成功したときには、 .I "*lineptr" と .I "*n" がバッファのアドレスと割り当てたサイズを反映した値に更新される。 .\"O .BR getdelim () .\"O works like .\"O .BR getline (), .\"O except that a line delimiter other than newline can be specified as the .\"O .I delimiter .\"O argument. .BR getdelim () は .BR getline () と同じように動作するが、改行文字以外の区切り文字を引き数 .I delim に指定することができる。 .\"O As with .\"O .BR getline (), .\"O a delimiter character is not added if one was not present .\"O in the input before end of file was reached. .BR getline () と同様に、ファイル終端に達するまでに入力行に区切り文字が見付からない場合は、 区切り文字をバッファに追加しない。 .\"O .SH "RETURN VALUE" .SH 返り値 .\"O On success, .\"O .BR getline () .\"O and .\"O .BR getdelim () .\"O return the number of characters read, including the delimiter character, .\"O but not including the terminating null byte. .\"O This value can be used .\"O to handle embedded null bytes in the line read. 成功した場合、 .BR getline () と .BR getdelim () は読み込んだ文字数を返す。 文字数には区切り文字は含まれるが、終端に使う NULL バイトは含まれない。 この値によって、読み込んだ行に含まれる NULL バイトを操作することができる。 .\"O Both functions return \-1 on failure to read a line (including end-of-file .\"O condition). どちらの関数も、行の読み込みに失敗した場合には \-1 を返す (ファイルの終端に達した場合にも \-1 を返す)。 .\"O .SH ERRORS .SH エラー .TP .B EINVAL .\"O Bad arguments .\"O .RI ( n .\"O or .\"O .I lineptr .\"O is NULL, or .\"O .I stream .\"O is not valid). 引き数が不正である .RI ( n または .I lineptr が NULL である。 もしくは .I stream が有効でない)。 .\"O .SH VERSIONS .SH バージョン .\"O These functions are available since libc 4.6.27. これらの関数は libc 4.6.27 以降で利用可能である。 .\"O .SH "CONFORMING TO" .SH 準拠 .\"O Both .\"O .BR getline () .\"O and .\"O .BR getdelim () .\"O were originally GNU extensions. .\"O They were standardized in POSIX.1-2008. .BR getline () と .BR getdelim () は、どちらも元は GNU による拡張であったが、 POSIX.1-2008 で標準化された。 .\"O .SH "EXAMPLE" .SH 例 .nf #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 :\en", read); printf("%s", line); } free(line); exit(EXIT_SUCCESS); } .fi .\"O .SH "SEE ALSO" .SH 関連項目 .BR read (2), .BR fgets (3), .BR fopen (3), .BR fread (3), .BR gets (3), .BR scanf (3)