diff options
Diffstat (limited to 'devdocs/c/io%2Fftell.html')
| -rw-r--r-- | devdocs/c/io%2Fftell.html | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/devdocs/c/io%2Fftell.html b/devdocs/c/io%2Fftell.html new file mode 100644 index 00000000..c2696fde --- /dev/null +++ b/devdocs/c/io%2Fftell.html @@ -0,0 +1,77 @@ + <h1 id="firstHeading" class="firstHeading">ftell</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"> <td class="t-dcl-nopad"> <pre data-language="c">long ftell( FILE *stream );</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>Returns the file position indicator for the file stream <code>stream</code>.</p> +<p>If the stream is open in binary mode, the value obtained by this function is the number of bytes from the beginning of the file.</p> +<p>If the stream is open in text mode, the value returned by this function is unspecified and is only meaningful as the input to <code><a href="fseek" title="c/io/fseek">fseek()</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> +</table> <h3 id="Return_value"> Return value</h3> <p>File position indicator on success or <code>-1L</code> if failure occurs.</p> +<p>On error, the <code>errno</code> variable is set to implementation-defined positive value.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> +<p>Demonstrates <code>ftell()</code> with error checking. Writes then reads a few floating-point (FP) values to/from a file.</p> +<div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <stdlib.h> + +/* If the condition is not met then exit the program with error message. */ +void check(_Bool condition, const char* func, int line) +{ + if (condition) + return; + perror(func); + fprintf(stderr, "%s failed in file %s at line # %d\n", func, __FILE__, line - 1); + exit(EXIT_FAILURE); +} + +int main(void) +{ + /* Prepare an array of FP values. */ + #define SIZE 5 + double A[SIZE] = {1.1,2.,3.,4.,5.}; + + /* Write array to a file. */ + const char* fname = "/tmp/test.bin"; + FILE* file = fopen(fname, "wb"); + check(file != NULL, "fopen()", __LINE__); + + const int write_count = fwrite(A, sizeof(double), SIZE, file); + check(write_count == SIZE, "fwrite()", __LINE__); + + fclose(file); + + /* Read the FP values into array B. */ + double B[SIZE]; + file = fopen(fname, "rb"); + check(file != NULL, "fopen()", __LINE__); + + long int pos = ftell(file); /* position indicator at start of file */ + check(pos != -1L, "ftell()", __LINE__); + printf("pos: %ld\n", pos); + + const int read_count = fread(B, sizeof(double), 1, file); /* read one FP value */ + check(read_count == 1, "fread()", __LINE__); + + pos = ftell(file); /* position indicator after reading one FP value */ + check(pos != -1L, "ftell()", __LINE__); + printf("pos: %ld\n", pos); + printf("B[0]: %.1f\n", B[0]); /* print one FP value */ + + return EXIT_SUCCESS; +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">pos: 0 +pos: 8 +B[0]: 1.1</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.21.9.4 The ftell function (p: 337-338) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.19.9.4 The ftell function (p: 303-304) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.9.9.4 The ftell 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="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/ftell" title="cpp/io/c/ftell">C++ documentation</a></span> for <code>ftell</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/ftell" class="_attribution-link">https://en.cppreference.com/w/c/io/ftell</a> + </p> +</div> |
