C标准库 setbuf函数

setbuf()函数用于将指定缓冲区与特定文件流相关联,实现操作缓冲区时直接操作了文件流的功能。

setbuf()函数 语法

void setbuf(FILE*p,char*bp);

参数p为要处理的流;

参数bp为要处理的缓冲区。

setbuf()函数没有返回值。

setbuf()函数 示例

本示例使用setbuf函数给标准文件流指定缓冲区outbuf,其具体代码如下所示:

#include<stdio.h>
char outbuf[BUFSIZ];
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. ");
     fflush(stdout);/*刷新缓冲区*/
     return 0;
}

运行结果如图所示。

setbuf()函数 示例


赞(1)
未经允许不得转载:极客笔记 » C标准库 setbuf函数

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址