setbufsetbuf

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

语法Syntax

void setbuf(
   FILE *stream,
   char *buffer
);

参数Parameters

指向文件结构。stream Pointer to FILE structure.

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

备注Remarks

Setbuf函数控制缓冲为The setbuf function controls buffering for stream. 参数必须引用未读取或写入打开的文件。The stream argument must refer to an open file that has not been read or written. 如果缓冲区自变量是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.

要求Requirements

例程Routine 必需的标头Required header
setbufsetbuf <stdio.h><stdio.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// 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" );
      _fcloseall();
   }
}
stream1 set to user-defined buffer at: 0012FCDC
stream2 buffering disabled

请参阅See also

流 I/OStream I/O
fclose、_fcloseallfclose, _fcloseall
fflushfflush
fopen、_wfopen_wfopenfopen, _wfopen
setvbufsetvbuf