词条 | setlinebuf |
释义 | 函数功能: 设置文件流为线性缓冲区 相关函数: setbuffer,setbuf,setvbuf 表头文件: #include<stdio.h> 定义函数: 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 functionfflush(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 <stdio.h> 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; } |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。