Use C setbuf function to set buffer for stream

Syntax

C setbuf function has the following syntax.

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

C setbuf function is from header file stdio.h.

Description

C setbuf function specifies the buffer that stream will use or, Set buf to null to turn off buffering.

The buffer must be BUFSIZ characters long. BUFSIZ is defined in stdio.h.

Example

Specify buffer for stream using C setbuf function.


#include <stdio.h>
/*from w  w w .ja  va2 s  . c om*/
int main ()
{
  char buffer[BUFSIZ];
  FILE *fp1, *fp2;

  fp1=fopen ("test.txt","w");
  fp2=fopen ("test2.txt","a");

  setbuf ( fp1 , buffer );
  fputs ("This is sent to a buffered stream",fp1);
  fflush (fp1);

  setbuf ( fp2 , NULL );
  fputs ("This is sent to an unbuffered stream",fp2);

  fclose (fp1);
  fclose (fp2);

  return 0;

}




















Home »
  C Language »
    Function Reference »




assert.h
ctype.h
math.h
setjmp.h
signal.h
stdio.h
stdlib.h
string.h
time.h
wctype.h