Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>setvbuf() control stream buffering and buffer size</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 setvbuf()               Control Stream Buffering and Buffer Size

 #include   <stdio.h>

 int        setvbuf(stream,buf,type,size);
 FILE       *stream;                     Pointer to file structure
 char       *buf;                        User-allocated buffer
 int        type;                        Type of buffer
 size_t     size;                        Size of buffer

    setvbuf() causes 'buf' to be used for I/O buffering instead of the
    automatically allocated buffer, thereby giving the user a way to
    control both buffering and buffer size for a given stream.  'stream'
    must refer to an open file.  If 'buf' is NULL, the buffer is
    allocated with malloc(). 'size' is used as the amount allocated.
    'size' must be greater than zero.

    If 'stream' is buffered, 'type' must be set to one of the following:

         _IONBF     No buffer is used, regardless of arguments 'buf' or
                    'size'
         _IOFBF     Full buffering (unless 'buf' is NULL). Use 'buf' as
                    the buffer and 'size' as the buffer size
         _IOLBF     Line buffering. On output the buffer is flushed
                    whenever a new-line character is written to the file.

     If 'type' is _IOFBF or _IOLBF, 'size' is used as the size of 'buf'.
    'size' must be greater than 0 and less than or equal to 32767.  If
    _IONBF is specified, then the stream is unbuffered and 'size' and
    'buf' are ignored.

       Returns:     0, if successful.  Non-zero is returned if an illegal
                    type or buffer size is specified.

   -------------------------------- Example ---------------------------------

    The following statements set up a user-specified buffer for one of
    the files opened.

           #include <stdio.h>

           FILE *stream1, *stream2;
           char *buf;
           int x;

           main()
           {
               if ((stream1 = fopen("data","w+")) != NULL &&
                   (stream2 = fopen("update","w+")) != NULL) {
                       if((x = setvbuf(stream2,buf,_IOFBF,512)) == 0)
                           printf("buffer set\n");
                       else
                           printf("unable to set buffer\n");
               }
           }


See Also: setbuf() fflush() fopen() fclose()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson