summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Fsetvbuf.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%2Fsetvbuf.html
new repository
Diffstat (limited to 'devdocs/c/io%2Fsetvbuf.html')
-rw-r--r--devdocs/c/io%2Fsetvbuf.html85
1 files changed, 85 insertions, 0 deletions
diff --git a/devdocs/c/io%2Fsetvbuf.html b/devdocs/c/io%2Fsetvbuf.html
new file mode 100644
index 00000000..25309406
--- /dev/null
+++ b/devdocs/c/io%2Fsetvbuf.html
@@ -0,0 +1,85 @@
+ <h1 id="firstHeading" class="firstHeading">setvbuf</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">int setvbuf( FILE * stream, char * buffer,
+ int mode, size_t size );</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">int setvbuf( FILE *restrict stream, char *restrict buffer,
+ int mode, size_t size );</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 _IOFBF /*unspecified*/
+#define _IOLBF /*unspecified*/
+#define _IONBF /*unspecified*/</pre>
+</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Changes the buffering mode of the given file stream <code>stream</code> as indicated by the argument <code>mode</code>. In addition,</p>
+<ul>
+<li> If <code>buffer</code> is a null pointer, resizes the internal buffer to <code>size</code>. </li>
+<li> If <code>buffer</code> is not a null pointer, instructs the stream to use the user-provided buffer of size <code>size</code> beginning at <code>buffer</code>. The stream must be closed (with <code><a href="fclose" title="c/io/fclose">fclose</a></code>) before the <a href="../language/lifetime" title="c/language/lifetime">lifetime</a> of the array pointed to by <code>buffer</code> ends. The contents of the array after a successful call to <code>setvbuf</code> are indeterminate and any attempt to use it is undefined behavior. </li>
+</ul> <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 or null pointer to change size and mode only </td>
+</tr> <tr class="t-par"> <td> mode </td> <td> - </td> <td> buffering mode to use. It can be one of the following values: <table class="wikitable"> <tr> <td> <a href="http://en.cppreference.com/w/c/io"><span class="kw892">_IOFBF</span></a> </td> <td> full buffering </td>
+</tr> <tr> <td> <a href="http://en.cppreference.com/w/c/io"><span class="kw893">_IOLBF</span></a> </td> <td> line buffering </td>
+</tr> <tr> <td> <a href="http://en.cppreference.com/w/c/io"><span class="kw894">_IONBF</span></a> </td> <td> no buffering </td>
+</tr>
+</table> </td>
+</tr> <tr class="t-par"> <td> size </td> <td> - </td> <td> size of the buffer </td>
+</tr>
+</table> <h3 id="Return_value"> Return value</h3> <p><code>​0​</code> on success or nonzero on failure.</p>
+<h3 id="Notes"> Notes</h3> <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><a href="setbuf" title="c/io/setbuf">setbuf</a></code>/<code>setvbuf</code>).</p>
+<p>Not all <code>size</code> bytes will necessarily be used for buffering: the actual buffer size is usually rounded down to a multiple of 2, a multiple of page size, etc.</p>
+<p>On many implementations, line buffering is only available for terminal input streams.</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> <p>The default buffer size <code><a href="../io" title="c/io">BUFSIZ</a></code> is expected to be the most efficient buffer size for file I/O on the implementation, but POSIX <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fstat.html">fstat</a> often provides a better estimate.</p>
+<h3 id="Example"> Example</h3> <div class="t-example">
+<p>One use case for changing buffer size is when a better size is known. (This example uses some POSIX function, e.g. <a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/7908799/xsh/fileno.html"><code>fileno</code></a>. See also SO: <a rel="nofollow" class="external text" href="https://stackoverflow.com/questions/15749184/fileno-not-available">#1</a> and <a rel="nofollow" class="external text" href="https://stackoverflow.com/questions/44622827/why-am-i-getting-error-implicit-declaration-of-function-fileno-when-i-try-t">#2</a>).</p>
+<div class="c source-c"><pre data-language="c">// Make some POSIX functions, such as `int fileno(FILE*)`, visible:
+#define _POSIX_SOURCE
+
+#include &lt;stdio.h&gt;
+#include &lt;stdlib.h&gt;
+#include &lt;sys/stat.h&gt;
+
+int main(void)
+{
+ FILE* fp = fopen("/tmp/test.txt", "w+");
+ if (fp == NULL)
+ {
+ perror("fopen");
+ return EXIT_FAILURE;
+ }
+
+ struct stat stats;
+ if (fstat(fileno(fp), &amp;stats) == -1) // POSIX only
+ {
+ perror("fstat");
+ return EXIT_FAILURE;
+ }
+
+ printf("BUFSIZ is %d, but optimal block size is %ld\n", BUFSIZ, stats.st_blksize);
+ if (setvbuf(fp, NULL, _IOFBF, stats.st_blksize) != 0)
+ {
+ perror("setvbuf failed"); // POSIX version sets errno
+ return EXIT_FAILURE;
+ }
+
+ int ch;
+ while((ch=fgetc(fp)) != EOF); // read entire file: use truss/strace to
+ // observe the read(2) syscalls used
+
+ fclose(fp);
+ return EXIT_SUCCESS;
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">BUFSIZ is 8192, but optimal block size is 65536</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul><li> 7.21.5.6 The setvbuf function (p: 225) </li></ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul><li> 7.21.5.6 The setvbuf function (p: 308) </li></ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 7.19.5.6 The setvbuf function (p: 273-274) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 4.9.5.6 The setvbuf function </li></ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="setbuf" title="c/io/setbuf"> <span class="t-lines"><span>setbuf</span></span></a></div> </td> <td> sets the buffer 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/setvbuf" title="cpp/io/c/setvbuf">C++ documentation</a></span> for <code>setvbuf</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/setvbuf" class="_attribution-link">https://en.cppreference.com/w/c/io/setvbuf</a>
+ </p>
+</div>