.\" Copyright (c) 2001 John Levon .\" Based in part on GNU libc documentation .\" .\" %%%LICENSE_START(VERBATIM) .\" 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_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" 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 .\" Updated 2013-07-22, Akihiro MOTOKI .\" .TH GETLINE 3 2015\-01\-22 GNU "Linux Programmer's Manual" .SH 名前 getline, getdelim \- 区切り文字までの文字列入力を読み込む .SH 書式 .nf \fB#include \fP .sp \fBssize_t getline(char **\fP\fIlineptr\fP\fB, size_t *\fP\fIn\fP\fB, FILE *\fP\fIstream\fP\fB);\fP \fBssize_t getdelim(char **\fP\fIlineptr\fP\fB, size_t *\fP\fIn\fP\fB, int \fP\fIdelim\fP\fB, FILE *\fP\fIstream\fP\fB);\fP .fi .sp .in -4n glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .in .sp .ad l \fBgetline\fP(), \fBgetdelim\fP(): .PD 0 .RS 4 .TP 4 glibc 2.10 以降: _POSIX_C_SOURCE\ >=\ 200809L || _XOPEN_SOURCE\ >=\ 700 .TP glibc 2.10 より前: _GNU_SOURCE .RE .PD .ad .SH 説明 \fBgetline\fP() は \fIstream\fP から 1 行全てを読み込み、テキストが含まれているバッファーのアドレスを \fI*lineptr\fP に格納する。 バッファーはヌル文字 (\e0) で終端される。 改行文字が見つかった場合は、改行文字もバッファーに格納される。 \fI*lineptr\fP が NULL にセットされ、 \fI*n\fP が呼び出し前に 0 にセットされた場合、 \fBgetline\fP() は行の内容を格納するためのバッファーを確保する。 このバッファーは、 \fBgetline\fP() が失敗した場合であっても、ユーザーのプログラムで解放すべきである。 別の方法として、 \fBgetline\fP() を呼び出す際に、 \fI*lineptr\fP に \fBmalloc\fP(3) で確保した大きさ \fI*n\fP バイトのバッファーへのポインターを入れて渡すこともできる。 読み込んだ行を保持するのに十分なバッファーがない場合、 \fBgetline\fP() は \fBrealloc\fP(3) を使ってバッファーのサイズを変更し、必要に応じて \fI*lineptr\fP と \fI*n\fP を更新する。 どちらの場合でも、呼び出しに成功したときには、 \fI*lineptr\fP と \fI*n\fP がバッファーのアドレスと割り当てたサイズを反映した値に更新される。 \fBgetdelim\fP() は \fBgetline\fP() と同じように動作するが、改行文字以外の区切り文字を引き数 \fIdelim\fP に指定することができる。 \fBgetline\fP() と同様に、ファイル終端に達するまでに入力行に区切り文字が見付からない場合は、 区切り文字をバッファーに追加しない。 .SH 返り値 成功した場合、 \fBgetline\fP() と \fBgetdelim\fP() は読み込んだ文字数を返す。 文字数には区切り文字は含まれるが、終端に使うヌルバイト (\(aq\e0\(aq) は含まれない。 この値によって、読み込んだ行に含まれるヌルバイトを操作することができる。 どちらの関数も、行の読み込みに失敗した場合には \-1 を返す (ファイルの終端に達した場合にも \-1 を返す)。 エラーが発生した場合には、 \fIerrno\fP にエラーの原因を示す値が設定される。 .SH エラー .TP \fBEINVAL\fP 引き数が不正である (\fIn\fP または \fIlineptr\fP が NULL である。 もしくは \fIstream\fP が有効でない)。 .SH 準拠 \fBgetline\fP() と \fBgetdelim\fP() は、どちらも元は GNU による拡張であったが、 POSIX.1\-2008 で標準化された。 .SH 例 .nf #define _GNU_SOURCE #include #include int main(void) { FILE *stream; char *line = NULL; size_t len = 0; ssize_t read; stream = fopen("/etc/motd", "r"); if (stream == NULL) exit(EXIT_FAILURE); while ((read = getline(&line, &len, stream)) != \-1) { printf("Retrieved line of length %zu :\en", read); printf("%s", line); } free(line); fclose(stream); exit(EXIT_SUCCESS); } .fi .SH 関連項目 \fBread\fP(2), \fBfgets\fP(3), \fBfopen\fP(3), \fBfread\fP(3), \fBscanf\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。