setbuf关闭流缓冲

来源:互联网 发布:招plc编程人 编辑:程序博客网 时间:2024/06/01 07:18

setbuf

打开和关闭缓冲机制。
功 能: 把缓冲区与流相联
用 法: void setbuf(FILE *steam, char *buf);
说明:setbuf函数具有打开和关闭缓冲机制。为了带缓冲进行I/O,参数buf必须指向一个长度为BUFSIZ(定义在stdio.h头文件中)的缓冲区。通常在此之后该流就是全缓冲的,但是如果该流与一个终端设备相关,那么某些系统也可以将其设置为行缓冲。

为了关闭缓冲,可以将buf参数设置为NULL。
setbuf(stderr, NULL);

#include <stdio.h>
char outbuf[50];
int main(void)
{
setbuf(stdout,outbuf);

puts("This is a test of buffered output.");
puts("This output will go into outbuf");
puts("and won't appear until the buffer");
puts("fills up or we flush the stream.\n");

puts(outbuf);

fflush(stdout);
return 0;
}
puts()函数用来向标准输出设备(屏幕)写字符串并换行,其调用方式为,puts(s);其中s为字符串字符(字符串数组名或字符串指针)