setbuf
控制流缓冲。 此函数已弃用;使用 setvbuf。
void setbuf(
FILE *stream,
char *buffer
);
参数
stream
指向 FILE 结构的指针。buffer
用户分配缓冲区。
备注
setbuf 函数控制 stream的缓冲。 stream 参数必须引用未读取或未编写打开的文件。 如果 buffer 参数为 NULL,不缓冲的流。 否则,必须指向 BUFSIZ缓冲区长度字符数组,BUFSIZ 是缓冲区大小。在 STDIO.H. 定义。 用户指定的缓冲区,而不是特定流的默认系统分配的缓冲区,用于缓存使用 I/O。 stderr 是一个非缓冲的流,但默认情况下,可以使用 setbuf 将缓冲区为 stderr。
setbuf 由 setvbuf替换为,是新代码的首选例程。 setbuf为现有的代码提供兼容性。
要求
例程 |
必需的标头 |
---|---|
setbuf |
<stdio.h> |
有关其他兼容性信息,请参见“简介”中的兼容性。
示例
// 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();
}
}
.NET Framework 等效项
不适用。若要调用标准 C 函数,请使用 PInvoke。有关更多信息,请参见平台调用示例。