diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/io%2Ffwrite.html | |
new repository
Diffstat (limited to 'devdocs/c/io%2Ffwrite.html')
| -rw-r--r-- | devdocs/c/io%2Ffwrite.html | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/devdocs/c/io%2Ffwrite.html b/devdocs/c/io%2Ffwrite.html new file mode 100644 index 00000000..35104178 --- /dev/null +++ b/devdocs/c/io%2Ffwrite.html @@ -0,0 +1,59 @@ + <h1 id="firstHeading" class="firstHeading">fwrite</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">size_t fwrite( const void* buffer, size_t size, size_t count, + FILE* stream );</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">size_t fwrite( const void* restrict buffer, size_t size, size_t count, + FILE* restrict stream );</pre> +</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <p>Writes <code>count</code> of objects from the given array <code>buffer</code> to the output stream <code>stream</code>. The objects are written as if by reinterpreting each object as an array of <code>unsigned char</code> and calling <code><a href="fputc" title="c/io/fputc">fputc</a></code> <code>size</code> times for each object to write those <code>unsigned char</code>s into <code>stream</code>, in order. The file position indicator for the stream is advanced by the number of characters written.</p> +<p>If an error occurs, the resulting value of the file position indicator for the stream is indeterminate.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> buffer </td> <td> - </td> <td> pointer to the first object in the array to be written </td> +</tr> <tr class="t-par"> <td> size </td> <td> - </td> <td> size of each object </td> +</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> the number of the objects to be written </td> +</tr> <tr class="t-par"> <td> stream </td> <td> - </td> <td> pointer to the output stream </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>The number of objects written successfully, which may be less than <code>count</code> if an error occurs.</p> +<p>If <code>size</code> or <code>count</code> is zero, <code>fwrite</code> returns zero and performs no other action.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +enum { SIZE = 5 }; + +int main(void) +{ + double a[SIZE] = {1, 2, 3, 4, 5}; + FILE* f1 = fopen("file.bin", "wb"); + assert(f1); + size_t r1 = fwrite(a, sizeof a[0], SIZE, f1); + printf("wrote %zu elements out of %d requested\n", r1, SIZE); + fclose(f1); + + double b[SIZE]; + FILE* f2 = fopen("file.bin", "rb"); + size_t r2 = fread(b, sizeof b[0], SIZE, f2); + fclose(f2); + printf("read back: "); + for (size_t i = 0; i < r2; ++i) + printf("%0.2f ", b[i]); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">wrote 5 elements out of 5 requested +read back: 1.00 2.00 3.00 4.00 5.00</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul><li> 7.21.8.2 The fwrite function (p: TBD) </li></ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.21.8.2 The fwrite function (p: TBD) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.21.8.2 The fwrite function (p: 335-336) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.19.8.2 The fwrite function (p: 301-302) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.9.8.2 The fwrite function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fprintf" title="c/io/fprintf"> <span class="t-lines"><span>printf</span><span>fprintf</span><span>sprintf</span><span>snprintf</span><span>printf_s</span><span>fprintf_s</span><span>sprintf_s</span><span>snprintf_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> prints formatted output to <code><a href="std_streams" title="c/io/std streams">stdout</a></code>, a file stream or a buffer <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="fputs" title="c/io/fputs"> <span class="t-lines"><span>fputs</span></span></a></div> </td> <td> writes a character string to a file stream <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="fread" title="c/io/fread"> <span class="t-lines"><span>fread</span></span></a></div> </td> <td> reads from a file <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/fwrite" title="cpp/io/c/fwrite">C++ documentation</a></span> for <code>fwrite</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/fwrite" class="_attribution-link">https://en.cppreference.com/w/c/io/fwrite</a> + </p> +</div> |
