setlinebuf

setlinebuf,是用來設定檔案流以換行為依據的緩衝IO,即行緩衝。

函式功能: 設定檔案流為線性緩衝區
相關函式: setbuffer,setbuf, setvbuf
表頭檔案: #include
定義函式: void setlinebuf(FILE * stream);
函式說明: setlinebuf()用來設定檔案流以換行為依據的緩衝IO,即行緩衝。
相當於調用setvbuf(stream,(char * )NULL,_IOLBF,0);請參考setvbuf()。
返回值:成功返回0,失敗返回任何非零值。
SYNOPSIS
#include < stdio.h>
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_tsize);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode ,size_t size);
DESCRIPTION
The three types of buffering available areunbuffered, block buffered, and line buffered. When an output stream isunbuffered, information appears on the Destination file or terminal as soon aswritten; when it is block buffered many characters are saved up and written asa block; when it is line buffered characters are saved up until a newline isoutput or input is read from any stream attached to a terminal device (typicallystdin). The function fflush(3)may be used to force the block out early. (See fclose(3).) Normally allfiles 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 errorstream stderr is always unbuffered by default.
The setvbuf function may be usedon any open stream to change its buffer. The modeparameter must be one ofthe following three macros:
_IONBF
unbuffered
_IOLBF
line buffered
_IOFBF
fully buffered
Except for unbuffered files,the buf argument should point to a buffer atleast size bytes long; this buffer will be used instead of thecurrent buffer. If the argument buf is NULL, only the mode isaffected; a new buffer will be allocated on the next read or write operation.The setvbuf function may only be used after opening a stream andbefore any other operations have been performed on it.
The other three calls are, in effect,simply aliases for calls to setvbuf. The setbuf function isexactly equivalent to the call
setvbuf(stream, buf, buf ? _IOFBF : _IONBF,BUFSIZ);
The setbuffer function is thesame, except that the size of the buffer is up to the caller, rather than beingdetermined by the default BUFSIZ. The setlinebuf function isexactly equivalent to the call:
setvbuf(stream, (char *)NULL, _IOLBF, 0);
RETURN VALUE
Thefunction setvbuf returns 0 on success. It can return any value onfailure, but returns nonzero when mode is invalid or the requestcannot be honoured. It may set errno on failure. The other functionsare void.
程式例:
#include
int main(void)
{
FILE *input;
output = fopen("file.out", "w");
/* set up output stream for line buffering using space that
will be obtained through an indirect call to malloc */
setlinebuf(output);
/* perform file I/O here */
/* close files */
fclose(output);
return 0;
}

相關詞條

相關搜尋

熱門詞條

聯絡我們