.\" Copyright (c) 1980, 1991 Regents of the University of California. .\" All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" the American National Standards Committee X3, on Information .\" Processing Systems. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" @(#)setbuf.3 6.10 (Berkeley) 6/29/91 .\" .\" Converted for Linux, Mon Nov 29 14:55:24 1993, faith@cs.unc.edu .\" Added section to BUGS, Sun Mar 12 22:28:33 MET 1995, .\" Thomas.Koenig@ciw.uni-karlsruhe.de .\" Correction, Sun, 11 Apr 1999 15:55:18, .\" Martin Vicente .\" Correction, 2000-03-03, Andreas Jaeger .\" Added return value for setvbuf, aeb, .\" .TH SETBUF 3 2001-06-09 "Linux" "Linux Programmer's Manual" .SH NAME setbuf, setbuffer, setlinebuf, setvbuf \- 流緩衝操作 .SH "SYNOPSIS 總覽" .na .B #include .sp .BI "void setbuf(FILE *" stream ", char *" buf ); .br .BI "void setbuffer(FILE *" stream ", char *" buf ", size_t " size ); .br .BI "void setlinebuf(FILE *" stream ); .br .BI "int setvbuf(FILE *" stream ", char *" buf ", int " mode .BI ", size_t " size ); .ad .SH "DESCRIPTION 描述" 有三種類型的緩衝策略,它們是無緩衝,塊緩衝和行緩衝。當輸出流無緩衝時,信息在寫的同時出現於目標文件或終端上;當是塊緩衝時,字符被暫存,然後一起寫入;當是行緩衝時,字符被暫存,直到要輸出一個新行符,或者從任何與終端設備連接的流中 (典型的是 stdin) 讀取輸入時才輸出。函數 .BR fflush (3) 可以用來強制提前輸出。(參見 .BR fclose (3)) 通常所有文件都是塊緩衝的。當文件 I/O 操作在文件上發生時,將調用 .BR malloc (3) ,獲得一個緩衝。如果流指向一個終端 (通常 .I stdout 都是這樣),那麼它是行緩衝的。標準錯誤流 .I stderr 默認總是無緩衝的。 .PP 函數 .B setvbuf 可以用在任何打開的流上,改變它的緩衝。參數 .I mode 必須是下列三個宏之一: .RS .TP .B _IONBF 無緩衝 .TP .B _IOLBF 行緩衝 .TP .B _IOFBF 完全緩衝 .RE .PP 除非是無緩衝的文件,否則參數 .I buf 應當指向一個長度至少爲 .I size 字節的緩衝;這個緩衝將取代當前的緩衝。如果參數 .I buf 是 .BR NULL ,只有這個模式會受到影響;下次 read 或 write 操作還將分配一個新的緩衝。函數 .B setvbuf 只能在打開一個流,還未對它進行任何其他操作之前使用。 .PP 其他三個函數調用是函數 .BR setvbuf 的別名,函數 .B setbuf 與使用下列語句完全等價: .PP .RS setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ); .RE .PP 函數 .B setbuffer 與此相同,但是緩衝的長度由用戶決定,而不是由默認值 .BR BUFSIZ 決定。函數 .B setlinebuf 與使用下列語句完全等價: .PP .RS setvbuf(stream, (char *)NULL, _IOLBF, 0); .RE .SH "RETURN VALUE 返回值" 函數 .B setvbuf 成功執行時返回 0。它失敗時可能返回任何值,但是當 It can return any value on failure, but returns nonzero when .I mode 不正確,或者不能實現請求時,必須返回非零值。它在失敗時可能設置 .I errno 。其他函數沒有返回值。 .SH "CONFORMING TO 標準參考" 函數 .B setbuf 和 .B setvbuf 遵循 ANSI X3.159-1989 (``ANSI C'') 標準。 .SH BUGS 函數 .B setbuffer 和 .B setlinebuf 無法移植到 4.2BSD 之前的 BSD 版本,在 Linux 中僅在 libc 4.5.21 之後的系統中可用。在 4.2BSD 和 4.3BSD 系統中, .B setbuf 總是使用非最優的緩衝大小,應當避免使用它。 .P 在 .I stream 被關閉時,必須確保 .I buf 和它指向的空間仍然存在。這通常發生在程序終止時。 .P 例如,下列調用是非法的: .nf .sp #include int main() { char buf[BUFSIZ]; setbuf(stdin, buf); printf("Hello, world!\\n"); return 0; } .fi .sp .SH "SEE ALSO 參見" .BR fclose (3), .BR fflush (3), .BR fopen (3), .BR fread (3), .BR malloc (3), .BR printf (3), .BR puts (3) .SH "跋" .br 本頁面中文版由中文 man 手冊頁計劃提供。 .br 中文 man 手冊頁計劃:\fBhttps://github.com/man-pages-zh/manpages-zh\fR