setbuf, setbuffer, setlinebuf, setvbuf - stream buffering operation

SYNOPSIS Overview

#include <stdio.h>

void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_t size);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode , size_t size);

DESCRIPTION Description

There are three types of buffering strategies are unbuffered, block buffer, and a buffer line. When unbuffered output stream, while the message is written to the destination file or terminal; when a block buffer, the character is temporarily stored, and then written together; when a line buffer, the character is temporarily stored, to be outputted until the a newline or read input from any stream when the output connected to the terminal device (typically stdin). Function fflush (. 3) may be used to force the output in advance. (See fclose (. 3)) generally blocks all files are buffered. When a file I / O operation occurs on a file, calls the malloc (. 3), a buffer is obtained. If the stream refers to a terminal (typically stdout are), then it is a line buffer. The standard error stream stderr default is always unbuffered.

Function setvbuf can be used on any open stream to change its buffer. Parameter mode must be one of the following three macros:

_IONBF
Unbuffered
_IOLBF
Line buffer
_IOFBF
Fully Buffered

Except for unbuffered file, or parameters buf should point to a length of at least size bytes; this buffer will replace the current buffer. If the argument buf is NULL , only the mode is affected; next read or write operation will also allocate a new buffer. Function setvbuf can open a stream, it has not been carried out before any other operations using.

The other three function calls is a function of setvbuf aliases, function setbuf with the use of the following statements is exactly equivalent to:

 

setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);

Function setbuffer and the same, but the length of the buffer is determined by the user, rather than the default value BUFSIZ determined. Function setlinebuf with the use of the following statements is exactly equivalent to:

 

setvbuf(stream, (char *)NULL, _IOLBF, 0);

RETURN VALUE The

Function setvbuf returns 0 if successful execution. It may return any value when it fails, but when the any return value ON CAN It failure, returns A nonzero But the when the MODE incorrect or can not fulfill the request, must return a nonzero value. It may be set in case of failure errno . Other function does not return a value.

CONFORMING TO standard reference

Function setbuf and setvbuf follow ANSI X3.159-1989 ( `` ANSI C ' ') standards.

BUGS

Function setbuffer and setlinebuf are not portable to versions of BSD before 4.2BSD, only available in the system after the 4.5.21 libc in Linux. In 4.2BSD and 4.3BSD systems, Setbuf always uses the optimum buffer size, and should be avoided. In the stream is closed, it must ensure that buf and the space it points to still exist. This usually happens when the program terminates. For example, the following call is illegal:

#include <stdio.h>
int main()
{
    char buf[BUFSIZ];
    setbuf(stdin, buf);
    printf("Hello, world!\n");
    return 0;
}

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11098467.html