summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Ffread.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%2Ffread.html
new repository
Diffstat (limited to 'devdocs/c/io%2Ffread.html')
-rw-r--r--devdocs/c/io%2Ffread.html70
1 files changed, 70 insertions, 0 deletions
diff --git a/devdocs/c/io%2Ffread.html b/devdocs/c/io%2Ffread.html
new file mode 100644
index 00000000..890f4374
--- /dev/null
+++ b/devdocs/c/io%2Ffread.html
@@ -0,0 +1,70 @@
+ <h1 id="firstHeading" class="firstHeading">fread</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">size_t fread( 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 fread( 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>Reads up to <code>count</code> objects into the array <code>buffer</code> from the given input stream <code>stream</code> as if by calling <code><a href="fgetc" title="c/io/fgetc">fgetc</a></code> <code>size</code> times for each object, and storing the results, in the order obtained, into the successive positions of <code>buffer</code>, which is reinterpreted as an array of <code>unsigned char</code>. The file position indicator for the stream is advanced by the number of characters read.</p>
+<p>If an error occurs, the resulting value of the file position indicator for the stream is indeterminate. If a partial element is read, its value 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 array where the read objects are stored </td>
+</tr> <tr class="t-par"> <td> size </td> <td> - </td> <td> size of each object in bytes </td>
+</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> the number of the objects to be read </td>
+</tr> <tr class="t-par"> <td> stream </td> <td> - </td> <td> the stream to read </td>
+</tr>
+</table> <h3 id="Return_value"> Return value</h3> <p>Number of objects read successfully, which may be less than <code>count</code> if an error or end-of-file condition occurs.</p>
+<p>If <code>size</code> or <code>count</code> is zero, <code>fread</code> returns zero and performs no other action.</p>
+<p><code>fread</code> does not distinguish between end-of-file and error, and callers must use <code><a href="feof" title="c/io/feof">feof</a></code> and <code><a href="ferror" title="c/io/ferror">ferror</a></code> to determine which occurred.</p>
+<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
+
+enum { SIZE = 5 };
+
+int main(void)
+{
+ const double a[SIZE] = {1.0, 2.0, 3.0, 4.0, 5.0};
+ printf("Array has size %ld bytes, element size: %ld\n", sizeof a, sizeof *a);
+ FILE *fp = fopen("test.bin", "wb"); // must use binary mode
+ fwrite(a, sizeof *a, SIZE, fp); // writes an array of doubles
+ fclose(fp);
+
+ double b[SIZE];
+ fp = fopen("test.bin","rb");
+ const size_t ret_code = fread(b, sizeof b[0], SIZE, fp); // reads an array of doubles
+ if (ret_code == SIZE)
+ {
+ printf("Array at %p read successfully, contents:\n", (void*)&amp;a);
+ for (int n = 0; n != SIZE; ++n)
+ printf("%f ", b[n]);
+ putchar('\n');
+ }
+ else // error handling
+ {
+ if (feof(fp))
+ printf("Error reading test.bin: unexpected end of file\n");
+ else if (ferror(fp))
+ perror("Error reading test.bin");
+ }
+
+ fclose(fp);
+}</pre></div> <p>Possible output:</p>
+<div class="text source-text"><pre data-language="c">Array has size 40 bytes, element size: 8
+Array at 0x1337f00d6960 read successfully, contents:
+1.000000 2.000000 3.000000 4.000000 5.000000</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C23 standard (ISO/IEC 9899:2023): </li>
+<ul><li> 7.21.8.1 The fread function (p: TBD) </li></ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul><li> 7.21.8.1 The fread function (p: 243-244) </li></ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul><li> 7.21.8.1 The fread function (p: 335) </li></ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 7.19.8.1 The fread function (p: 301) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 4.9.8.1 The fread function </li></ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fscanf" title="c/io/fscanf"> <span class="t-lines"><span>scanf</span><span>fscanf</span><span>sscanf</span><span>scanf_s</span><span>fscanf_s</span><span>sscanf_s</span></span></a></div>
+<div><span class="t-lines"><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> reads formatted input from <code><a href="std_streams" title="c/io/std streams">stdin</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="fgets" title="c/io/fgets"> <span class="t-lines"><span>fgets</span></span></a></div> </td> <td> gets a character string from a file stream <br> <span class="t-mark">(function)</span> </td>
+</tr> <tr class="t-dsc"> <td> <div><a href="fwrite" title="c/io/fwrite"> <span class="t-lines"><span>fwrite</span></span></a></div> </td> <td> writes to 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/fread" title="cpp/io/c/fread">C++ documentation</a></span> for <code>fread</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/fread" class="_attribution-link">https://en.cppreference.com/w/c/io/fread</a>
+ </p>
+</div>