Misplaced Pages

Setvbuf: Difference between revisions

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editContent deleted Content addedVisualWikitext
Revision as of 07:35, 22 July 2008 editNicM (talk | contribs)4,492 edits tweak intro sentences← Previous edit Latest revision as of 23:40, 27 April 2023 edit undoSteel1943 (talk | contribs)Autopatrolled, Extended confirmed users, Page movers, Pending changes reviewers, Rollbackers, Template editors197,606 edits Rcat 
(18 intermediate revisions by 8 users not shown)
Line 1: Line 1:
#REDIRECT ]
{{lowercase|setvbuf}}
In computing, <code>'''setvbuf'''</code> is a ] in ] which lets the programmer control the buffering of a file stream. It is declared in <]>. <code>setvbuf</code> and its function prototype is:


{{Redirect category shell|
<source lang="c">int setvbuf(FILE *stream, char *buf, int mode, size_t size);</source>
{{R with history}}

{{R to anchor}}
Where ''stream'' is a pointer to the file stream for which the relevant buffering operations will be performed; ''buf'' is a char array of ''size'' length or a ]; and ''mode'' is the kind of buffering desired: <code>_IOFBF</code>, for fully buffered, <code>_IOLBF</code> for line buffered and <code>_IONBF</code> for unbuffered. These three macros are defined in <stdio.h>. <code>setvbuf</code> returns zero on success or nonzero on failure.
}}

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

<source lang="c">void setbuf(FILE *stream, char *buf);</source>

<code>setbuf</code>'s behavior is equivalent to:

<source lang="c">(void)setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);</source>

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

== Example ==

The output of this program should be ''Hello world'' followed by a newline.
<source lang="c">
#include <stdio.h>
#include <stdlib.h>

int main(void) {
char buf;

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('\n'); /* stdout is line buffered; everything in the buffer is now written to stdout along with the newline */

return EXIT_SUCCESS;
}
</source>

== See also ==
* <code>]</code>
* ]

]

Latest revision as of 23:40, 27 April 2023

Redirect to:

This page is a redirect. The following categories are used to track and monitor this redirect:
  • With history: This is a redirect from a page containing substantive page history. This page is kept as a redirect to preserve its former content and attributions. Please do not remove the tag that generates this text (unless the need to recreate content on this page has been demonstrated), nor delete this page.
    • This template should not be used for redirects having some edit history but no meaningful content in their previous versions, nor for redirects created as a result of a page merge (use {{R from merge}} instead), nor for redirects from a title that forms a historic part of Misplaced Pages (use {{R with old history}} instead).
  • To an embedded anchor: This is a redirect from a topic that does not have its own page to an embedded anchor on the redirect's target page.
    • An {{anchor|(anchor name)}} or {{visible anchor}} template, a HTML element with id="(anchor name)", or an |id=(anchor name) parameter might be installed at the beginning of a paragraph, in or near a section header or within a table. The anchor might also be an old section header that has been edited and is anchored within or near the new header to prevent broken internal and external links.
    • Even though section headers of the general form ==(Header name)== are themselves a type of anchor, use {{R to section}} instead.
When appropriate, protection levels are automatically sensed, described and categorized.
Setvbuf: Difference between revisions Add topic