setbufのヘルプ・マニュアル
日本語 英語
setbuf --help
man setbuf
SETBUF(3) Linux Programmer’s Manual SETBUF(3)
名前
setbuf, setbuffer, setlinebuf, setvbuf - ストリームのバッファリングの操
作
書式
#include
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
setbuffer(), setlinebuf(): _BSD_SOURCE
説明
バッファリングには unbuffered, block buffered, line buffered の3つの タ
イ プがある。出力ストリームのタイプが unbuffered の場合、データを書き込
むとすぐに出力先ファイルに書き込まれるかターミナルに表示され る 。block
buffered の場合、文字の読み書きはブロック単位でいっぺんに行われる。line
buffered の場合、新しい行が出力されるか、ターミナルデバイスに接続してい
るストリーム (通常、stdin) から新しい行が入力されるまで文字がたくわえら
れる。ブロックを強制的に出力するには fflush(3) 関数を使う。 (fclose(3)
を 参照のこと) 通常、ファイルはすべて block buffered である。ファイルに
対して初めて入出力処理を行うと malloc(3) が呼び出されバッファが獲得され
る。もし ストリームが (通常、 stdout がそうであるように) ターミナルを参
照する場合には、ファイルは line buffered となる。標準エラー出力 stderr
はデフォルトでは常に unbuffered である。
setvbuf() 関数は、オープンしている任意のストリームに対してバッファを変
更できる。引き数 mode は、次の 3 つのマクロのうちいずれかである:
_IONBF unbuffered
_IOLBF line buffered
_IOFBF fully buffered
unbuffered のファイルを除き、 buf 引数は size バイト以上の大きさのバ ッ
フ ァを指していなければならない。このバッファは現在のバッファの代わりに
用いられる。もし、引数 buf が NULL ならば、モードだけが変更される。新し
いバッファは次に読み書きした際に割り当てられる。 setvbuf() 関数は、スト
リームをオープンした後、そのストリームに対して何らかの操作をする前に の
み使用できる。
他 の 3 つの関数は setvbuf() の呼び出しに単純に置き換えることができる。
setbuf() 関数は、
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
と全く同等だし、 setbuffer() 関数は、バッファサイズがデフォルト値 BUF-
SIZ ではなく引数で与えられる点以外は同じである。 setlinebuf() 関数は
setvbuf(stream, (char *) NULL, _IOLBF, 0);
と同じである。
返り値
setvbuf() 関数は、成功した場合 0 を返す。失敗した場合、0 以外の値を返す
(失敗とは、 mode が不正な場合またはリクエストが条件を満たさない場合であ
る)。 setvbuf() 関数が失敗した場合は errno を設定することもある。
その他の関数は値を返さない。
準拠
setbuf() 関数および setvbuf() 関数は C89 と C99 に準拠している。
バグ
setbuffer() 関数および setlinebuf() 関数は 4.2BSD より前の BSD とは互換
性がない。また Linux でも(古いバージョンでは)利用できないかもしれ な い
。4.2BSD および 4.3BSD のシステムでは setbuf() は必ず追加のバッファーの
サイズを使用するので、これも使うべきでない。
stream を閉じる時 (プログラムを終了する際にもこれは起きる) には 、 buf
が 指し示す空間とが存在していることを保証しなければならない。例えば、次
のような使い方は許されない:
#include
int
main(void)
{
char buf[BUFSIZ];
setbuf(stdin, buf);
printf("Hello, world!\n");
return 0;
}
関連項目
fclose(3), fflush(3), fopen(3), fread(3), malloc(3), printf(3), puts(3)
Linux 2008-06-26 SETBUF(3)
SETBUF(3) Linux Programmer’s Manual SETBUF(3)
NAME
setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operations
SYNOPSIS
#include
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode, size_t size);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
setbuffer(), setlinebuf(): _BSD_SOURCE
DESCRIPTION
The three types of buffering available are unbuffered, block buffered,
and line buffered. When an output stream is unbuffered, information
appears on the destination file or terminal as soon as written; when it
is block buffered many characters are saved up and written as a block;
when it is line buffered characters are saved up until a newline is
output or input is read from any stream attached to a terminal device
(typically stdin). The function fflush(3) may be used to force the
block out early. (See fclose(3).) Normally all files are block
buffered. When the first I/O operation occurs on a file, malloc(3) is
called, and a buffer is obtained. If a stream refers to a terminal (as
stdout normally does) it is line buffered. The standard error stream
stderr is always unbuffered by default.
The setvbuf() function may be used on any open stream to change its
buffer. The mode argument must be one of the following three macros:
_IONBF unbuffered
_IOLBF line buffered
_IOFBF fully buffered
Except for unbuffered files, the buf argument should point to a buffer
at least size bytes long; this buffer will be used instead of the cur-
rent buffer. If the argument buf is NULL, only the mode is affected; a
new buffer will be allocated on the next read or write operation. The
setvbuf() function may only be used after opening a stream and before
any other operations have been performed on it.
The other three calls are, in effect, simply aliases for calls to
setvbuf(). The setbuf() function is exactly equivalent to the call
setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
The setbuffer() function is the same, except that the size of the
buffer is up to the caller, rather than being determined by the default
BUFSIZ. The setlinebuf() function is exactly equivalent to the call:
setvbuf(stream, (char *) NULL, _IOLBF, 0);
RETURN VALUE
The function setvbuf() returns 0 on success. It returns non-zero on
failure (mode is invalid or the request cannot be honored). It may set
errno on failure.
The other functions do not return a value.
CONFORMING TO
The setbuf() and setvbuf() functions conform to C89 and C99.
BUGS
The setbuffer() and setlinebuf() functions are not portable to versions
of BSD before 4.2BSD, and are available under Linux since libc 4.5.21.
On 4.2BSD and 4.3BSD systems, setbuf() always uses a suboptimal buffer
size and should be avoided.
You must make sure that the space that buf points to still exists by
the time stream is closed, which also happens at program termination.
For example, the following is invalid:
#include
int
main(void)
{
char buf[BUFSIZ];
setbuf(stdin, buf);
printf("Hello, world!\n");
return 0;
}
SEE ALSO
fclose(3), fflush(3), fopen(3), fread(3), malloc(3), printf(3), puts(3)
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/.
Linux 2008-06-26 SETBUF(3)