Skip to content

Latest commit

 

History

History
110 lines (81 loc) · 3.13 KB

File metadata and controls

110 lines (81 loc) · 3.13 KB
description title ms.date api_name api_location api_type topic_type f1_keywords helpviewer_keywords ms.assetid
Learn more about: fgetpos
fgetpos
4/2/2020
fgetpos
_o_fgetpos
msvcrt.dll
msvcr80.dll
msvcr90.dll
msvcr100.dll
msvcr100_clr0400.dll
msvcr110.dll
msvcr110_clr0400.dll
msvcr120.dll
msvcr120_clr0400.dll
ucrtbase.dll
api-ms-win-crt-stdio-l1-1-0.dll
DLLExport
apiref
fgetpos
fgetpos function
streams, file position indicator
bfa05c38-1135-418c-bda1-d41be51acb62

fgetpos

Gets a stream's file-position indicator.

Syntax

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

Parameters

stream
Target stream.

pos
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 isn't a valid file pointer or isn't 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.

Remarks

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.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Function Required header
fgetpos <stdio.h>

For more compatibility information, see Compatibility.

Example

// 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 );
}

Input: crt_fgetpos.txt

fgetpos gets a stream's file-position indicator.

Output crt_fgetpos.txt

after fgetpos: gets a stream
after fsetpos: gets a stream

See also

Stream I/O
fsetpos