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.

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 or the value of pos is invalid, such as if either is a null pointer. If stream or pos is a NULL pointer, the function invokes the invalid parameter handler, as described in Parameter Validation.

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



ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

// crt_fgetpos.c
// This program uses fgetpos and fsetpos to
// return to a location in a file.

#include <stdio.h>

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

   if( fopen_s( &stream, "crt_fgetpos.txt", "rb" ) ) {
      perror( "Trouble opening file" );
      return -1;

   // Read some data and then save the position. 
   fread( buffer, sizeof( char ), 8, stream );
   if( fgetpos( stream, &pos ) != 0 ) {
      perror( "fgetpos error" );
      return -1;

   fread( buffer, sizeof( char ), 13, stream );
   printf( "after fgetpos: %.13s\n", buffer );

   // Restore to old position and read data 
   if( fsetpos( stream, &pos ) != 0 ) {
      perror( "fsetpos error" );
      return -1;

   fread( buffer, sizeof( char ), 13, stream );
   printf( "after fsetpos: %.13s\n", buffer );
   fclose( stream );

fgetpos gets a stream's file-position indicator.

Output crt_fgetpos.txt

after fgetpos: gets a stream
after fsetpos: gets a stream
© 2015 Microsoft