summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Ffsetpos.html
blob: 3ef15100b401ef794121602f57d3df095c310b02 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    <h1 id="firstHeading" class="firstHeading">fsetpos</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"> <td class="t-dcl-nopad"> <pre data-language="c">int fsetpos( FILE *stream, const fpos_t *pos );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p>Sets the file position indicator and the multibyte parsing state (if any) for the file stream <code>stream</code> according to the value pointed to by <code>pos</code>.</p>
<p>Besides establishing new parse state and position, a call to this function undoes the effects of <code><a href="ungetc" title="c/io/ungetc">ungetc</a></code> and clears the end-of-file state, if it is set.</p>
<p>If a read or write error occurs, the error indicator (<code><a href="ferror" title="c/io/ferror">ferror</a></code>) for the stream is set.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> stream </td> <td> - </td> <td> file stream to modify </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 use as new value of file position indicator </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>​0​</code> upon success, nonzero value otherwise.</p>
<h3 id="Notes"> Notes</h3> <p>After seeking to a non-end position in a wide stream, the next call to any output function may render the remainder of the file undefined, e.g. by outputting a multibyte sequence of a different length.</p>
<h3 id="Example"> Example</h3> <div class="t-example">
<p>fsetpos with error checking</p>
<div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
 
int main(void)
{
    /* Prepare an array of f-p values. */
    #define SIZE 5
    double A[SIZE] = {1.,2.,3.,4.,5.};
    /* Write array to a file. */
    FILE * fp = fopen("test.bin", "wb");
    fwrite(A,sizeof(double),SIZE,fp);
    fclose (fp);
 
    /* Read the f-p values into array B. */
    double B[SIZE];
    fp = fopen("test.bin","rb");
    fpos_t pos;
    if (fgetpos(fp,&amp;pos) != 0)      /* current position: start of file */
    {
       perror("fgetpos()");
       fprintf(stderr,"fgetpos() failed in file %s at line # %d\n", __FILE__,__LINE__-3);
       exit(EXIT_FAILURE);
    }
 
    int ret_code = fread(B,sizeof(double),1,fp);   /* read one f-p value */
    /* current position: after reading one f-p value */
    printf("%.1f; read count = %d\n", B[0], ret_code);   /* print one f-p value and ret_code */
 
    if (fsetpos(fp,&amp;pos) != 0)   /* reset current position to start of file */
    {
       if (ferror(fp))
       {
          perror("fsetpos()");
          fprintf(stderr,"fsetpos() failed in file %s at line # %d\n", __FILE__,__LINE__-5);
          exit(EXIT_FAILURE);
       }
    }
 
    ret_code = fread(B,sizeof(double),1,fp);   /* reread first f-p value */
    printf("%.1f; read count = %d\n", B[0], ret_code);   /* print one f-p value and ret_code */
    fclose(fp);
 
    return EXIT_SUCCESS; 
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">1.0; read count = 1
1.0; read count = 1</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.21.9.3 The fsetpos function (p: 337) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.19.9.3 The fsetpos function (p: 303) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.9.9.3 The fsetpos function </li></ul>
</ul>         <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fgetpos" title="c/io/fgetpos"> <span class="t-lines"><span>fgetpos</span></span></a></div> </td> <td> gets the file position indicator <br> <span class="t-mark">(function)</span>  </td>
</tr> <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 colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/fsetpos" title="cpp/io/c/fsetpos">C++ documentation</a></span> for <code>fsetpos</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/fsetpos" class="_attribution-link">https://en.cppreference.com/w/c/io/fsetpos</a>
  </p>
</div>