Gets a stream's file-position indicator.
#include <stdio.h>
int fgetpos( FILE *stream, fpos_t *pos );
stream | Target stream | |
pos | Position-indicator storage |
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 the fgetpos and fsetpos functions.
If successful, the fgetpos function returns 0. On failure, it returns a nonzero value and sets errno to one of the following manifest constants (defined in STDIO.H):
Constant | Meaning |
EBADF | The specified stream is not a valid file handle or is not accessible. |
EINVAL | The stream value is invalid. |
Standards:ANSI
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:DOS32X
/* FGETPOS.C: This program opens a file and reads bytes at several
* different locations.
*/
#include <stdio.h>
void main( void )
{
FILE *stream;
fpos_t pos;
int val;
char buffer[20];
if( (stream = fopen( "fgetpos.c", "rb" )) == NULL )
printf( "Trouble opening file\n" );
else
{
/* Read some data and then check the position. */
fread( buffer, sizeof( char ), 10, stream );
if( fgetpos( stream, &pos ) != 0 )
perror( "fgetpos error" );
else
{
fread( buffer, sizeof( char ), 10, stream );
printf( "10 bytes at byte %ld: %.10s\n", pos, buffer );
}
/* Set a new position and read more data */
pos = 140;
if( fsetpos( stream, &pos ) != 0 )
fread( buffer, sizeof( char ), 10, stream );
printf( "10 bytes at byte %ld: %.10s\n", pos, buffer );
fclose( stream );
}
}
10 bytes at byte 10: .C: This p
10 bytes at byte 140: FILE *