home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / SETBUF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  4.5 KB  |  97 lines

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