控制流缓冲。Controls stream buffering. 此函数已弃用;请改用 setvbufThis function is deprecated; use setvbuf instead.


void setbuf(  
   FILE *stream,  
   char *buffer   


指向 FILE 结构的指针。Pointer to FILE structure.

用户分配的缓冲区。User-allocated buffer.


setbuf 函数控制 stream 的缓冲。The setbuf function controls buffering for stream. stream 参数必须引用未读取或编写的打开的文件。The stream argument must refer to an open file that has not been read or written. 如果 buffer 参数为 NULL,则流是无缓冲的。If the buffer argument is NULL, the stream is un-buffered. 如果不是这样,则缓冲区必须指向长度为 BUFSIZ 的字符数组,其中 BUFSIZ 是 STDIO.H 中定义的缓冲区大小。If not, the buffer must point to a character array of length BUFSIZ, where BUFSIZ is the buffer size as defined in STDIO.H. 用户指定的缓冲区(而不是给定流的默认系统分配的缓冲区)用于 I/O 缓存。The user-specified buffer, instead of the default system-allocated buffer for the given stream, is used for I/O buffering. 默认情况下,stderr 流是无缓冲的,但您可以使用 setbuf 将缓存区分配给 stderrThe stderr stream is un-buffered by default, but you can use setbuf to assign buffers to stderr.

setbuf 已由新代码的首选例程 setvbuf 替换。setbuf has been replaced by setvbuf, which is the preferred routine for new code. 保留 setbuf 以便与现有代码兼容。setbuf is retained for compatibility with existing code.


例程所返回的值Routine 必需的标头Required header
setbuf <stdio.h><stdio.h>

有关其他兼容性信息,请参见“简介”中的 兼容性For additional compatibility information, see Compatibility in the Introduction.


// crt_setbuf.c  
// compile with: /W3  
// This program first opens files named DATA1 and  
// DATA2. Then it uses setbuf to give DATA1 a user-assigned  
// buffer and to change DATA2 so that it has no buffer.  

#include <stdio.h>  

int main( void )  
   char buf[BUFSIZ];  
   FILE *stream1, *stream2;  

   fopen_s( &stream1, "data1", "a" );  
   fopen_s( &stream2, "data2", "w" );  

   if( (stream1 != NULL) && (stream2 != NULL) )  
      // "stream1" uses user-assigned buffer:  
      setbuf( stream1, buf ); // C4996  
      // Note: setbuf is deprecated; consider using setvbuf instead  
      printf( "stream1 set to user-defined buffer at: %Fp\n", buf );  

      // "stream2" is unbuffered  
      setbuf( stream2, NULL ); // C4996  
      printf( "stream2 buffering disabled\n" );  
stream1 set to user-defined buffer at: 0012FCDC  
stream2 buffering disabled  

请参阅See Also

流 I/O Stream I/O
fclose、_fcloseall fclose, _fcloseall
fflush fflush
fopen、_wfopen fopen, _wfopen