setbuf - C stdio.h

C examples for stdio.h:setbuf

Type

function

From


<cstdio>
<stdio.h>

Description

Specifies buffer form the stream for I/O operations.

Prototype

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

Parameters

ParameterDescription
stream FILE object.
buffer User allocated buffer.

Return Value

none.

Example

In the following code, two files are opened for writing.

myfile1.txt is set to a user buffer until the fflush function is called.

myfile2.txt is set to unbuffered, so the subsequent output operation is written to the device as soon as possible.

closing a file flushes its buffer.

Demo Code


#include <stdio.h>

int main ()//from   ww  w.  j av a2  s .c o  m
{
  char buffer[BUFSIZ];
  FILE *pFile1, *pFile2;

  pFile1=fopen ("myfile1.txt","w");
  pFile2=fopen ("myfile2.txt","a");

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

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

  fclose (pFile1);
  fclose (pFile2);

  return 0;
}

Related Tutorials