setvbuf的使用

setvbuf

  函数名:  setvbuf
  功 能: 把缓冲区与流相关
  用 法:  int setvbuf(FILE *stream, char *buf, int type, unsigned size);
  参数:stream :指向流的指针 ;
  buf : 期望缓冲区的地址;
  type : 期望缓冲区的类型:
  _IOFBF(满缓冲):当缓冲区为空时,从流读入数据。或者当缓冲区满时,向流写入数 据。
  _IOLBF(行缓冲):每次从流中读入一行数据或向流中写入一行数据。
  _IONBF(无缓冲):直接从流中读入数据或直接向流中写入数据,而没有缓冲区。
  size : 缓冲区内字节的数量。
  注意:This function should be called once the file associated with the stream has already been opened but before any input or output operation has taken place.
  意思是这个函数应该在打开流后,立即调用,在任何对该流做输入输出前
  程序例:
   #include <stdio.h>
  int main(void)
  {
  FILE *input, *output;
  char bufr[512];
  input = fopen("file.in", "r+b");
  output = fopen("file.out", "w");
  /* set up input stream for minimal disk access,
  using our own character buffer */
  if (setvbuf(input, bufr, _IOFBF, 512) != 0)
  printf("failed to set up buffer for input file\n");
  else
  printf("buffer set up for input file\n");
  /* set up output stream for line buffering using space that
  will be obtained through an indirect call to malloc */
  if (setvbuf(output, NULL, _IOLBF, 132) != 0)
  printf("failed to set up buffer for output file\n");
  else
  printf("buffer set up for output file\n");
  /* perform file I/O here */
  /* close files */
  fclose(input);
  fclose(output);
  return 0;
  }
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值