summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Fsetbuf.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/io%2Fsetbuf.html
new repository
Diffstat (limited to 'devdocs/c/io%2Fsetbuf.html')
-rw-r--r--devdocs/c/io%2Fsetbuf.html46
1 files changed, 46 insertions, 0 deletions
diff --git a/devdocs/c/io%2Fsetbuf.html b/devdocs/c/io%2Fsetbuf.html
new file mode 100644
index 00000000..fb5c4a93
--- /dev/null
+++ b/devdocs/c/io%2Fsetbuf.html
@@ -0,0 +1,46 @@
+ <h1 id="firstHeading" class="firstHeading">setbuf</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdio.h&gt;</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 &lt;stdio.h&gt;
+#include &lt;threads.h&gt;
+
+int main(void)
+{
+ setbuf(stdout, NULL); // unbuffered stdout
+ putchar('a'); // 'a' appears immediately if stdout is unbuffered
+ thrd_sleep(&amp;(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">
+ &copy; 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>