Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - setvbuf http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   setvbuf

   Usage
   #include <stdio.h>

   int setvbuf(FILE *fp,char *buf, int mode,size_t size);

   Description
   setvbuf  specifies  the  type and size of a buffer to be  used  for  a
   stream.  In addition to the function parameters the  following  global
   variable affects the behaviour of this function: _okbigbuf.

   This  is used only in the T, S and M memory models, and  controls  how
   buffers  are allocated when buf is NULL. It is statically  initialized
   to  0  or 1 by the programmer (the default definition in  the  library
   sets  it to 1). If _okbigbuf is 1 and the memory model is T, S  or  M:
   setvbuf  tries  to allocate a buffer outside of the data  segment.  If
   that  fails,  and  size <= BUFSIZ, then setvbuf tries  to  allocate  a
   buffer within the data segment.

   A  buffer  that  is  outside the data segment  is  marked  by  setting
   _IOBIGBUF  in fp->_flags . If _okbigbuf is 0 or the memory model is  C
   or  L: setvbuf tries to allocate a buffer within the data  segment.  A
   buffer  allocated by setvbuf is flagged by _IOMYBUF being set  in  fp-
   >_flags.  See the section entitled  Zortech Implementation in  the
   Reference  Guide  for  more information  on  _okbigbufand  dynamic
   memory allocation.

   The function parameters are:
   fp        Stream pointer that is already opened, but before any  reads
             or writes have been done to the stream.
   buf       Pointer  to  buffer, or NULL. If NULL,  then  setvbuf  uses
             malloc  or  faralloc  to try to allocate a  buffer  of  size
             bytes.  If  buf  is not NULL, it points  to  a  buffer  that
             setvbuf will cause to be associated with the stream fp.

   mode      Is one of:
   _IONBF    No  buffering.  The buf and  size  parameters  are  ignored.
             Unbuffered  I/O  means  that: data  written  is  immediately
             passed on to DOS. When data is read, exactly enough is read.

   _IOLBF    Do  line  buffering. The actual I/O  is  performed  when  a
             newline is read or written.
   _IOFBF    Full buffering. Data is read a full buffer at a time.  Data
             is written only when the buffer is full.

   size      If buf is NULL, then size is the number of bytes to allocate
             for  the buffer. If buf is not NULL, then size must  be  the
             number of bytes in the buffer that buf points to.

   Example
   #include <stdio.h>

   main(argc,argv)
   int argc;
   char *argv[];
   {
   FILE *fp;
   static char buf[100];
                                      /* Make stdprn unbuffered */
        setvbuf(stdprn,NULL,_IONBF,0);
        fprintf(stdprn,"unbuffered\n");
        if(argc == 2)
                                      /* if an argument */
             fp = fopen(argv[1],"w");
        else
             fp = stdout;
                                      /* use standard output */
        if(setvbuf(fp,buf,_IOLBF,sizeof(buf)))
             printf("setvbuf failed\n");
        else
        {
             fprintf(fp,"This is going to fp\n");
             fclose(fp);
        }
   }

   Return Value
   If  success, the various fields that fp points to are updated to  show
   the buffer and 0 is returned. If there is insufficient memory for  the
   buffer or the mode parameter is invalid, a non-zero result is returned


See Also: setbuf

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