setbuf
void  setbuf ( FILE * stream , char * buffer );
stdio.h
  cplusplus.com  

Change stream buffering.
  Changes the buffer used for I/O operations with the specified stream, or, if the specified buffer is NULL it disables buffering with the stream.
  This function should be called once the file associated with the stream has been opened but before any input or output operation has been done.
  The buffer must point to an array BUFSIZ bytes long (BUFSIZ is defined in stdio.h).
  With buffered streams writing operations do not write directly to the device associated with them; the data is accumulated in the buffer and written to the device as a block. This can be forced flushing the stream by calling fflush or by closing the file (fclose). All buffers are also flushed when program terminates.
  With unbuffered streams, data is directly written to the physical device on each writing operation.
  Normally, files are opened with a default allocated buffer, where this function can be used to define a user-allocated buffer or to disable buffering for the file. System standard streams like stdout and stderr are unbuffered by default if they are not redirected.

Parameters.

stream
pointer to an open file.
buffer
User allocated buffer. Must have a length of BUFSIZ bytes.

Return Value.
  none

Example.

/* setbuf example */
#include <stdio.h>

main()
{
  char buffer[BUFSIZ];
  FILE *pFile1, *pFile2;

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

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

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

  fclose (pFile1);
  fclose (pFile2);

  return 0;
}

  Two files are opened with writing access. The stream associated with the file example1.txt is set to a user allocated buffer; a writing operation to it is done; the data is logically part of the stream now and a reading operation to it can get this data, but it has not been phisically writen to the device (hard drive) until the fflush has been called.
  The second buffer in the example, associated with the file example2.txt, is set to unbuffered, so the subsequent output operation is directly written to the device.

See also.
  fopen, fflush, setvbuf


© The C++ Resources Network, 2000