A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../../cpp/../cpp/../cpp/../cpp/../c/io/fgetpos.html below:

fgetpos - cppreference.com

(until C99) int fgetpos( FILE* restrict stream, fpos_t* restrict pos ); (since C99)

Obtains the file position indicator and the current parse state (if any) for the file stream stream and stores them in the object pointed to by pos. The value stored is only meaningful as the input to fsetpos.

[edit] Parameters stream - file stream to examine pos - pointer to a fpos_t object to store the file position indicator to [edit] Return value

​0​ upon success, nonzero value otherwise.

[edit] Example
#include <assert.h>
#include <stdio.h>
#include <stdlib.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");
}

Output:

First value in the file: 1.1
First value in the file again: 1.1
could not fsetpos stdin: Illegal seek
[edit] References
[edit] See also returns the current file position indicator
(function) [edit] moves the file position indicator to a specific location in a file
(function) [edit] moves the file position indicator to a specific location in a file
(function) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4