setvbuf - C stdio.h

C examples for stdio.h:setvbuf

Type

function

From


<cstdio>
<stdio.h>

Description

Change stream buffering by setting the mode and size of the buffer (in bytes).

Prototype

int setvbuf ( FILE * stream, char * buffer, int mode, size_t size );

Parameters

ParameterDescription
stream FILE object.
buffer User allocated buffer at least size bytes long.
mode a mode for file buffering.
size Buffer size, in bytes.

Three special macro constants:

modeDescription
_IOFBF Full buffering
_IOLBFLine buffering
_IONBFNo buffering.

Return Value

On success, a zero value is returned.

Otherwise, a non-zero value is returned.

Example

A full buffer of 1024 bytes is requested for the associated stream.

This stream should only be written to the file each time the 1024-byte buffer is filled.

Demo Code


#include <stdio.h>

int main ()//  w  ww  .  j  a v  a 2 s  . c  o  m
{
  FILE *pFile;

  pFile=fopen ("main.cpp","w");

  setvbuf ( pFile , NULL , _IOFBF , 1024 );

  // File operations here

  fclose (pFile);

  return 0;
}

Related Tutorials