home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / SETBUF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.1 KB  |  96 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - setbuf.c
  3.  *
  4.  * function(s)
  5.  *        setbuf - assigns buffering to a stream
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <stdio.h>
  18.  
  19. /*-----------------------------------------------------------------------*
  20.  
  21. Name            setbuf - assigns buffering to a stream
  22.  
  23. Usage           #include <stdio.h>
  24.                 void setbuf(FILE *stream, char *buf);
  25.  
  26. Related
  27. functions usage int setvbuf(FILE *stream, char *buf, int type,
  28.                             size_t size);
  29.  
  30. Prototype in    stdio.h
  31.  
  32. Description     setbuf and setvbuf cause the buffer buf to be used
  33.                 for I/O buffering instead of an automatically allocated buffer.
  34.                 They are used after the given stream is opened.
  35.  
  36.                 In setbuf, if buf is NULL, I/O will be unbuffered; otherwise,
  37.                 it will be fully buffered. The buffer must be BUFSIZ bytes
  38.                 long (specified in stdio.h). In setvbuf, if buf is NULL, a
  39.                 buffer will be allocated using malloc; the buffer will use
  40.                 size as the amount allocated. The size parameter specifies
  41.                 the buffer size and must be greater than zero.
  42.  
  43.                 stdin and stdout are unbuffered if they are not redirected;
  44.                 otherwise, they are fully buffered. setbuf may be used to
  45.                 change the buffering style being used.
  46.  
  47.                 Unbuffered means that characters written to a stream are
  48.                 immediately output to the file or device, while buffered
  49.                 means that the characters are accumulated and written as
  50.                 a block.
  51.  
  52.                 In setvbuf, the type parameter is one of the following:
  53.  
  54.                         _IOFBF  The file is fully buffered. When a buffer
  55.                                 is empty, the next input operation will
  56.                                 attempt to fill the entire buffer.
  57.  
  58.                                 On output the buffer will be completely
  59.                                 filled before any data is written to the file.
  60.  
  61.                         _IOLBF  The file is line buffered. When a buffer
  62.                                 is empty, the next input operation will still
  63.                                 attempt to fill the entire buffer. On output,
  64.                                 however, the buffer will be flushed whenever
  65.                                 a newline character is written to the file.
  66.  
  67.                         _IONBF  The file is unbuffered. The buf and size
  68.                                 parameters are ignored. Each input operation
  69.                                 will read directly from the file, and each
  70.                                 output operation will immediately write the
  71.                                 data to the file.
  72.  
  73.                 setbuf will produce unpredictable results if it is called for a
  74.                 stream, except immediately after opening the stream or any call
  75.                 to fseek. Calling setbuf after a stream has been unbuffered is
  76.                 legal and will not cause problems.
  77.  
  78.                 A common cause for error is to allocate the buffer as an
  79.                 automatic (local) variable and then fail to close the file
  80.                 before returning from the function where the buffer was
  81.                 declared.
  82.  
  83. Return value    setbuf returns nothing.
  84.  
  85.                 setvbuf returns 0 on success. It returns non-zero if an
  86.                 invalid value is given for type or size, if buf is NULL,
  87.                 or if there is not enough space to allocate a buffer.
  88.  
  89.                 setvbuf returns 0 on success.
  90.  
  91. *-----------------------------------------------------------------------*/
  92. void setbuf(FILE *fp, char *buf)
  93. {
  94.         setvbuf (fp, buf, (buf != NULL) ? _IOFBF : _IONBF, BUFSIZ);
  95. }
  96.