Standard Library Functions in C

adplus-dvertising
setvbuf() Function
Previous Home Next

Description

The C library function, The setvbuf() function allows control over the buffering strategy and buffer size for a specified stream. The setvbuf() function only works in ILE C when using the integrated file system. The stream must refer to a file that has been opened, but not read or written to. The array pointed to by buf designates an area that you provide that the C library may choose to use as a buffer for the stream.

Declaration

Following is the declaration for setvbuf() function.

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

Parameters

stream - A pointer to a FILE object which identifies a stream.

buffer - This is pointer to a memory block to be used as buffer for given stream. The size of this buffer must be at least BUFSIZ byte

mode - This specifies a mode for file buffering -

mode description
_IOFBFFull buffering - On output, data is written once the buffer is full. On Input the buffer is filled when an input operation is requested and the buffer is empty.
_IOLBFLine buffering - On output, data is written when a newline character is inserted into the stream or when the buffer is full, what so ever happens first. On Input, the buffer is filled till the next newline character when an input operation is requested and buffer is empty.
_IONBFNo buffering - No buffer is used. Each I/O operation is written as soon as possible. The buffer and size parameters are ignored.

Return Value

The setvbuf() function returns 0 if successful. It returns nonzero if a value that is not valid was specified in the parameter list, or if the request cannot be performed.

Example

#include <stdio.h>

int main ()
{
  FILE *pFile;

  pFile=fopen ("myfile.txt","w");

  setvbuf ( pFile , NULL , _IOFBF , 1024 );

  // File operations here

  fclose (pFile);

  return 0;
}

Output

A file called myfile.txt is created and a full buffer of 1024 bytes is requested for the associated stream, so the data output to this stream should only be written to the file each time the 1024-byte buffer is filled.

Previous Home Next