diff options
Diffstat (limited to 'devdocs/c/io%2Ffgetpos.html')
| -rw-r--r-- | devdocs/c/io%2Ffgetpos.html | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/devdocs/c/io%2Ffgetpos.html b/devdocs/c/io%2Ffgetpos.html new file mode 100644 index 00000000..4a403e25 --- /dev/null +++ b/devdocs/c/io%2Ffgetpos.html @@ -0,0 +1,58 @@ + <h1 id="firstHeading" class="firstHeading">fgetpos</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">int fgetpos( FILE *stream, fpos_t *pos );</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 fgetpos( FILE *restrict stream, fpos_t *restrict pos );</pre> +</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> </table> <p>Obtains the file position indicator and the current parse state (if any) for the file stream <code>stream</code> and stores them in the object pointed to by <code>pos</code>. The value stored is only meaningful as the input to <code><a href="fsetpos" title="c/io/fsetpos">fsetpos</a></code>.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> stream </td> <td> - </td> <td> file stream to examine </td> +</tr> <tr class="t-par"> <td> pos </td> <td> - </td> <td> pointer to a <code><a href="fpos_t" title="c/io/fpos t">fpos_t</a></code> object to store the file position indicator to </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p><code>0</code> upon success, nonzero value otherwise.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <stdlib.h> +#include <assert.h> + +int main(void) +{ + // prepare a file holding 4 values of type double + enum {SIZE = 4}; + FILE* fp = fopen("test.bin", "wb"); + assert(fp); + int rc = fwrite((double[SIZE]){1.1, 2.2, 3.3, 4.4}, sizeof(double), SIZE, fp); + assert(rc == SIZE); + fclose(fp); + + // demo using fsetpos to return to the beginning of a file + fp = fopen("test.bin", "rb"); + fpos_t pos; + fgetpos(fp, &pos); // store start of file in pos + double d; + rc = fread(&d, sizeof d, 1, fp); // read the first double + assert(rc == 1); + printf("First value in the file: %.1f\n", d); + fsetpos(fp,&pos); // move file position back to the start of the file + rc = fread(&d, sizeof d, 1, fp); // read the first double again + assert(rc == 1); + printf("First value in the file again: %.1f\n", d); + fclose(fp); + + // demo error handling + rc = fsetpos(stdin, &pos); + if(rc) perror("could not fsetpos stdin"); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">First value in the file: 1.1 +First value in the file again: 1.1 +could not fsetpos stdin: Illegal seek</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.21.9.1 The fgetpos function (p: 336) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.19.9.1 The fgetpos function (p: 302) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.9.9.1 The fgetpos function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="ftell" title="c/io/ftell"> <span class="t-lines"><span>ftell</span></span></a></div> </td> <td> returns the current file position indicator <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="fseek" title="c/io/fseek"> <span class="t-lines"><span>fseek</span></span></a></div> </td> <td> moves the file position indicator to a specific location in a file <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="fsetpos" title="c/io/fsetpos"> <span class="t-lines"><span>fsetpos</span></span></a></div> </td> <td> moves the file position indicator to a specific location in 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/fgetpos" title="cpp/io/c/fgetpos">C++ documentation</a></span> for <code>fgetpos</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/fgetpos" class="_attribution-link">https://en.cppreference.com/w/c/io/fgetpos</a> + </p> +</div> |
