fgetpos()

Updated: April 19, 2023

Get the current position of a stream

Synopsis:

#include <stdio.h>

int fgetpos( FILE* fp, 
             fpos_t* pos );

Arguments:

fp
The stream whose position you want to determine.
pos
A pointer to a fpos_t object where the function can store the position.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fgetpos() function stores the current position of the stream fp in the fpos_t object specified by pos.

You can use the value stored in pos in a call to fsetpos() if you want to reposition the file to the position at the time of the fgetpos() call.

Returns:

0 for success, or nonzero if an error occurs (errno is set).

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    FILE *fp;
    fpos_t position;
    char buffer[80];

    fp = fopen( "file", "r" );
    if( fp != NULL ) {
        fgetpos( fp, &position ); /* get position     */
        fgets( buffer, 80, fp );      /* read record      */
        fsetpos( fp, &position ); /* set position     */
        fgets( buffer, 80, fp );      /* read same record */
        fclose( fp );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

Classification:

ANSI, POSIX 1003.1

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes