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> |
|
|
* ] |
|
|
|
|
|
] |
|