1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
<h1 id="firstHeading" class="firstHeading">setbuf</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdio.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-until-c99"> <td> <pre data-language="c">void setbuf( FILE *stream, char *buffer );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">void setbuf( FILE *restrict stream, char *restrict buffer );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">#define BUFSIZ /*unspecified*/</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Sets the internal buffer to use for stream operations. It should be at least <code>BUFSIZ</code> characters long.</p>
<p>If <code>buffer</code> is not null, equivalent to <code><a href="http://en.cppreference.com/w/c/io/setvbuf"><span class="kw823">setvbuf</span></a><span class="br0">(</span>stream, buffer, <a href="http://en.cppreference.com/w/c/io"><span class="kw892">_IOFBF</span></a>, <a href="http://en.cppreference.com/w/c/io"><span class="kw891">BUFSIZ</span></a><span class="br0">)</span></code>.</p>
<p>If <code>buffer</code> is null, equivalent to <code><a href="http://en.cppreference.com/w/c/io/setvbuf"><span class="kw823">setvbuf</span></a><span class="br0">(</span>stream, <a href="http://en.cppreference.com/w/c/types/NULL"><span class="kw103">NULL</span></a>, <a href="http://en.cppreference.com/w/c/io"><span class="kw894">_IONBF</span></a>, <span class="nu0">0</span><span class="br0">)</span></code>, which turns off buffering.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> stream </td> <td> - </td> <td> the file stream to set the buffer to </td>
</tr> <tr class="t-par"> <td> buffer </td> <td> - </td> <td> pointer to a buffer for the stream to use. If a null pointer is supplied, the buffering is turned off </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>None.</p>
<h3 id="Notes"> Notes</h3> <p>If <code><a href="../io" title="c/io">BUFSIZ</a></code> is not the appropriate buffer size, <code><a href="setvbuf" title="c/io/setvbuf">setvbuf</a></code> can be used to change it.</p>
<p><code><a href="setvbuf" title="c/io/setvbuf">setvbuf</a></code> should also be used to detect errors, since <code>setbuf</code> does not indicate success or failure.</p>
<p>This function may only be used after <code>stream</code> has been associated with an open file, but before any other operation (other than a failed call to <code>setbuf</code>/<code>setvbuf</code>).</p>
<p>A common error is setting the buffer of stdin or stdout to an array whose lifetime ends before the program terminates:</p>
<div class="c source-c"><pre data-language="c">int main(void) {
char buf[BUFSIZ];
setbuf(stdin, buf);
} // lifetime of buf ends, undefined behavior</pre></div> <h3 id="Example"> Example</h3> <div class="t-example">
<p><code>setbuf</code> may be used to disable buffering on streams that require immediate output.</p>
<div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <threads.h>
int main(void)
{
setbuf(stdout, NULL); // unbuffered stdout
putchar('a'); // 'a' appears immediately if stdout is unbuffered
thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec
putchar('b');
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">ab</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.21.5.5 The setbuf function (p: 225) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.21.5.5 The setbuf function (p: 307-308) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.19.5.5 The setbuf function (p: 273) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.9.5.5 The setbuf function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="setvbuf" title="c/io/setvbuf"> <span class="t-lines"><span>setvbuf</span></span></a></div> </td> <td> sets the buffer and its size for a file stream <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/setbuf" title="cpp/io/c/setbuf">C++ documentation</a></span> for <code>setbuf</code> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/io/setbuf" class="_attribution-link">https://en.cppreference.com/w/c/io/setbuf</a>
</p>
</div>
|