setvbuf [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Library Reference Manual
setvbuf
Assigns a buffer and buffering method to an open stream.
Syntax
#include <stdio.h>
int setvbuf (FILE *stream, char *buffer,
int type, size_t size);
Parameters
stream A pointer to an open stream.
buffer Either a pointer to a character array for buffered I/O, or
null.
type The method of buffering.
size The size of the buffer.
Return Values
0 Success.
!=0 An error occurred.
Description
The setvbuf function enables you to assign a character array for
buffering, and also provides the means to specify the size of the buffer
to be used (size) and the type of buffering to be done (type).
Acceptable values for type (defined in <stdio.h>) include:
_IOFBF Input/output is fully buffered.
_IOLBF Output is line buffered. The buffer is flushed
each time a new line is written, the buffer is
full, or input is requested.
_IONBF Input/output is completely unbuffered.
If type _IONBF is specified, stream is totally unbuffered. Because no
buffer is needed, values for buffer and size are ignored.
If buffer is the null pointer and type is specified as _IOFBF or _IOLBF,
setvbuf() automatically allocates a buffer of size bytes through a call
to malloc().
If size is zero, a buffer of size BUFSIZ is used. This behavior can be
used to change the buffer size for a stream even if you still want the
standard I/O system to automatically allocate the buffer. This is
particularly useful when a buffer larger than the specified BUFSIZ is
needed.
Examples
In the following examples, the following two calls, though different, are
functionally identical:
setvbuf(fp, NULL, _IONBF, 0)
setbuf(fp, NULL)
When type is _IOFBF or _IOLBF, buffering for stream is determined by
buffer and size. If buffer is not the null pointer, it must point to a
character array of size bytes. All buffering of stream is then handled
through this array.
:
FILE *fp;
char buffer [256]
char *filename;
int retcode;
fp=fopen(filename, "w");
retcode=setvbuf(fp, buffer, _IOFBF, 256);
if (retcode !=0) error ();
This fragment causes stream fp to be buffered through the 256-byte array
buffer. Serious run-time errors can occur if the buffer array is not the
size specified in the call to setvbuf() (here 256 bytes). As with
setbuf, it is dangerous to use an automatic array for the buffer. Note
that the return value of setvbuf() can be used to verify that the request
was completed successfully.
:
FILE * fp;
char * filename;
int retcode;
:
fp = fopen(filename, "rt")
retcode=setvbuf(fp, NULL, _IOFBF, 2048);
if(retcode !=0) error( );
This fragment buffers stream fp through a 2048-byte buffer that is
allocated by the system.
See Also
setbuf(), ANSI C 4.9.5.6
MPE/iX 5.0 Documentation