.\" 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. .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .TH GETLINE 3 2010\-06\-12 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 の場合、 \fBgetline\fP() は行の内容を格納するためのバッファを確保する。 このバッファはユーザーのプログラムで解放すべきである (この場合、 \fI*n\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() は読み込んだ文字数を返す。 文字数には区切り文字は含まれるが、終端に使う NULL バイトは含まれない。 この値によって、読み込んだ行に含まれる NULL バイトを操作することができる。 どちらの関数も、行の読み込みに失敗した場合には \-1 を返す (ファイルの終端に達した場合にも \-1 を返す)。 .SH エラー .TP \fBEINVAL\fP 引き数が不正である (\fIn\fP または \fIlineptr\fP が NULL である。 もしくは \fIstream\fP が有効でない)。 .SH バージョン これらの関数は libc 4.6.27 以降で利用可能である。 .SH 準拠 \fBgetline\fP() と \fBgetdelim\fP() は、どちらも元は GNU による拡張であったが、 POSIX.1\-2008 で標準化された。 .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 .SH 関連項目 \fBread\fP(2), \fBfgets\fP(3), \fBfopen\fP(3), \fBfread\fP(3), \fBgets\fP(3), \fBscanf\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.41 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。