1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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>
|