Export (0) Print
Expand All


Visual Studio 6.0

Sets the stream-position indicator.

int fsetpos( FILE *stream, const fpos_t *pos );

Function Required Header Compatibility
fsetpos <stdio.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.


LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

If successful, fsetpos returns 0. On failure, the function returns a nonzero value and sets errno to one of the following manifest constants (defined in ERRNO.H): EBADF, which means the file is not accessible or the object that stream points to is not a valid file handle; or EINVAL, which means an invalid stream value was passed.



Pointer to FILE structure


Position-indicator storage


The fsetpos function sets the file-position indicator for stream to the value of pos, which is obtained in a prior call to fgetpos against stream. The function clears the end-of-file indicator and undoes any effects of ungetc on stream. After calling fsetpos, the next operation on stream may be either input or output.


/* 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;
   char   buffer[20];

   if( (stream = fopen( "fgetpos.c", "rb" )) == NULL )
      printf( "Trouble opening file\n" );
      /* Read some data and then check the position. */
      fread( buffer, sizeof( char ), 10, stream );
      if( fgetpos( stream, &pos ) != 0 )
         perror( "fgetpos error" );
         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 )
      perror( "fsetpos error" );

   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:

Stream I/O Routines

See Also   fgetpos

© 2014 Microsoft