Setvbuf

Setvbuf

setvbuf is a function in standard C which lets the programmer control the buffering of a file stream. It is declared in <stdio.h>; its function prototype is:

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

"stream" is a pointer to the file stream for which the relevant buffering operations will be performed; "buf" is a char array of "size" in length, or a null pointer; and "mode" is the kind of buffering desired: _IOFBF, for fully buffered, _IOLBF for line buffered and _IONBF for unbuffered. These three macros are defined in . setvbuf returns zero on success or nonzero on failure.

A related function, setbuf also controls the buffering of a file stream. Unlike setvbuf, setbuf takes only two arguments. The prototype is:

void setbuf(FILE *stream, char *buf);

setbuf's behavior is equivalent to:

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

That is, if "buf" is not NULL, set the stream to fully buffered using the given buffer; otherwise, set the stream to unbuffered. If a buffer is provided to setbuf, it must be at least BUFSIZ bytes long. The function always succeeds.

Example

The output of this program should be "Hello world" followed by a newline.
#include
#include

int main(void) { char buf [42] ;

if(setvbuf(stdout, buf, _IOFBF, sizeof buf)) { perror("failed to change the buffer of stdout"); return EXIT_FAILURE; } printf("He"); /* The buffer contains "He"; nothing is written yet to stdout */ fflush(stdout); /* "He" is actually written to stdout */

if(setvbuf(stdout, NULL, _IONBF, 0)) { perror("failed to change the buffer of stdout"); return EXIT_FAILURE; } printf("llo w"); /* "llo w" is written to stdout, there is no buffering */

if(setvbuf(stdout, buf, _IOLBF, sizeof buf)) { perror("failed to change the buffer of stdout"); return EXIT_FAILURE; } printf("orld"); /* The buffer now contains "orld"; nothing is written yet to stdout */ putchar(' '); /* stdout is line buffered; everything in the buffer is now written to stdout along with the newline */

return EXIT_SUCCESS;}

See also

* fflush
* Data buffer


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Stdio.h — Saltar a navegación, búsqueda stdio.h, que significa standard input output header (cabecera estandar E/S), es la biblioteca estándar del lenguaje de programación C, el archivo de cabecera que contiene las definiciones de macros, las constantes,… …   Wikipedia Español

  • stdio.h — stdio.h, que significa standard input output header (cabecera estandar E/S), es la biblioteca estándar del lenguaje de programación C, el archivo de cabecera que contiene las definiciones de macros, las constantes, las declaraciones de funciones… …   Wikipedia Español

  • C file input/output — C Standard Library Data types Character classification Strings Mathematics File input/output Date/time Localiza …   Wikipedia

  • Stdio.h — Стандартная библиотека языка программирования С assert.h complex.h ctype.h errno.h fenv.h float.h inttypes.h iso646.h limits.h locale.h math.h setjmp.h signal.h stdarg.h stdbool.h stddef.h stdint.h stdio.h stdlib.h …   Википедия

  • stdio.h — Стандартная библиотека языка программирования С assert.h complex.h ctype.h errno.h fenv.h float.h inttypes.h iso646.h limits.h locale.h math.h setjmp.h signal.h stdarg.h stdbool.h stddef.h …   Википедия

  • List of C functions — This page aims to alphabetically list all the predefined functions used in the C standard library, and a few of the non standard functions. * assert.h ** (no functions) * ctype.h ** (non standard) digittoint ** isalnum ** isalpha ** (non… …   Wikipedia

  • Fflush — is a C function belonging to the ANSI C standard library, and included in the file stdio.h. Its purpose is to write any buffered output data for the specified stream. Function prototype :int fflush(FILE *stream pointer );Argument meaning: *… …   Wikipedia

  • Funciones de la biblioteca estándar de C — Anexo:Funciones de la biblioteca estándar de C Saltar a navegación, búsqueda El propósito de este artículo es proporcionar un listado alfabético de todas las funciones de la biblioteca estándar de C, y unas pocas funciones no estándar. Contenido… …   Wikipedia Español

  • Список функций стандартной библиотеки Си — В этой статье в алфавитном порядке перечислены все предопределенные функции, используемые в стандартной библиотеке Си, а также несколько нестандартных функций. assert.h (нет функций) ctype.h (нестандартная) digittoint isalnum isalpha… …   Википедия

  • Anexo:Funciones de la biblioteca estándar de C — El propósito de este artículo es proporcionar un listado alfabético de todas las funciones de la biblioteca estándar de C, y unas pocas funciones no estándar. Contenido 1 assert.h 2 ctype.h 3 errno.h 4 float.h …   Wikipedia Español

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”