user-0 提问: 请问setvbuf（）函数关联流和缓冲区的作用是什么

回复:
user: user-1
content: SETVBUF Section: POSIX Programmer's Manual (P) Updated: 2003  --------------------------------------------------------------------------------    NAME setvbuf - assign buffering to a stream    SYNOPSIS #include <stdio.h>    int setvbuf(FILE *restrict stream, char *restrict buf, int type,         size_t size);      DESCRIPTION The setvbuf() function may be used after the stream pointed to by stream is associated with an open file but before any other operation (other than an unsuccessful call to setvbuf()) is performed on the stream. The argument type determines how stream shall be buffered, as follows:   * {_IOFBF} shall cause input/output to be fully buffered.   * {_IOLBF} shall cause input/output to be line buffered.   * {_IONBF} shall cause input/output to be unbuffered.   If buf is not a null pointer, the array it points to may be used instead of a buffer allocated by setvbuf() and the argument size specifies the size of the array; otherwise, size may determine the size of a buffer allocated by the setvbuf() function. The contents of the array at any time are unspecified.   For information about streams, see Standard I/O Streams .     RETURN VALUE Upon successful completion, setvbuf() shall return 0. Otherwise, it shall return a non-zero value if an invalid value is given for type or if the request cannot be honored,  and may set errno to indicate the error.     ERRORS The setvbuf() function may fail if:   EBADF  The file descriptor underlying stream is not valid.   The following sections are informative.     EXAMPLES None.     APPLICATION USAGE A common source of error is allocating buffer space as an "automatic" variable in a code block, and then failing to close the stream in the same block.   With setvbuf(), allocating a buffer of size bytes does not necessarily imply that all of size bytes are used for the buffer area.   Applications should note that many implementations only provide line buffering on input from terminal devices.     RATIONALE None.     FUTURE DIRECTIONS None.     SEE ALSO Standard I/O Streams , fopen() , setbuf() , the Base Definitions volume of IEEE Std 1003.1-2001, <stdio.h>     COPYRIGHT Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .  --------------------------------------------------------------------------------

user: user-1
content: setvbuf Controls stream buffering and buffer size.  int setvbuf( FILE *stream, char *buffer, int mode, size_t size );  Routine Required Header Compatibility  setvbuf <stdio.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  setvbuf returns 0 if successful, or a nonzero value if an illegal type or buffer size is specified.  Parameters  stream  Pointer to FILE structure  buffer  User-allocated buffer  mode  Mode of buffering  size  Buffer size in bytes. Allowable range: 2 < size < 32768. Internally, the value supplied for size is rounded down to the nearest multiple of 2.  Remarks  The setvbuf function allows the program to control both buffering and buffer size for stream. stream must refer to an open file that has not undergone an I/O operation since it was opened. The array pointed to by buffer is used as the buffer, unless it is NULL, in which case setvbuf uses an automatically allocated buffer of length size/2 * 2 bytes.  The mode must be _IOFBF, _IOLBF, or _IONBF. If mode is _IOFBF or _IOLBF, then size is used as the size of the buffer. If mode is _IONBF, the stream is unbuffered and size and buffer are ignored. Values for mode and their meanings are:  _IOFBF  Full buffering; that is, buffer is used as the buffer and size is used as the size of the buffer. If buffer is NULL, an automatically allocated buffer size bytes long is used.  _IOLBF  With MS-DOS, the same as _IOFBF.  _IONBF  No buffer is used, regardless of buffer or size.  Example  /* SETVBUF.C: This program opens two streams: stream1  * and stream2. It then uses setvbuf to give stream1 a  * user-defined buffer of 1024 bytes and stream2 no buffer.  */  #include <stdio.h>  void main( void ) {    char buf[1024];    FILE *stream1, *stream2;     if( ((stream1 = fopen( "data1", "a" )) != NULL) &&        ((stream2 = fopen( "data2", "w" )) != NULL) )    {       if( setvbuf( stream1, buf, _IOFBF, sizeof( buf ) ) != 0 )          printf( "Incorrect type or size of buffer for stream1\n" );       else          printf( "'stream1' now has a buffer of 1024 bytes\n" );       if( setvbuf( stream2, NULL, _IONBF, 0 ) != 0 )          printf( "Incorrect type or size of buffer for stream2\n" );       else          printf( "'stream2' now has no buffer\n" );       _fcloseall();    } }   Output  'stream1' now has a buffer of 1024 bytes 'stream2' now has no buffer   Stream I/O Routines  See Also   fclose, fflush, fopen, setbuf

user: user-2
content: 另外指定一个缓冲区

user: user-0
content: 大神在哪啊。。。