Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Gets a stream's file-position indicator.

int fgetpos( 
   FILE *stream,
   fpos_t *pos 


Target stream.
Position-indicator storage.

Return Value

If successful, fgetpos returns 0. On failure, it returns a nonzero value and sets errno to one of the following manifest constants (defined in STDIO.H): EBADF, which means the specified stream is not a valid file pointer or is not accessible, or EINVAL, which means the stream value is invalid.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, return codes.


The fgetpos function gets the current value of the stream argument's file-position indicator and stores it in the object pointed to by pos. The fsetpos function can later use information stored in pos to reset the stream argument's pointer to its position at the time fgetpos was called. The pos value is stored in an internal format and is intended for use only by fgetpos and fsetpos.


Function Required header Compatibility
fgetpos <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_fgetpos.c
/* This program opens a file and reads
 * bytes at several different locations.

#include <stdio.h>

int main( void )
   FILE   *stream;
   fpos_t pos;
   char   buffer[20];

   if( (stream = fopen( "crt_fgetpos.txt", "rb" )) == NULL )
      printf( "Trouble opening file\n" );
      /* Read some data and then check the position. */
      fread( buffer, sizeof( char ), 5, stream );
      if( fgetpos( stream, &pos ) != 0 )
         perror( "fgetpos error" );
         fread( buffer, sizeof( char ), 10, stream );
         printf( "10 bytes at byte %I64d: %.10s\n", pos, buffer );

   /* Set a new position and read more data */
   pos = 14;
   if( fsetpos( stream, &pos ) != 0 )
      perror( "fsetpos error" );

   fread( buffer, sizeof( char ), 10, stream );
   printf( "10 bytes at byte %I64d: %.10s\n", pos, buffer );
   fclose( stream );

Input: crt_fgetpos.txt

This is the input for crt_fgetpos.


10 bytes at byte 5: is the inp
10 bytes at byte 14: put for cr

See Also

Stream I/O Routines | fsetpos | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft