fsetpos

Description

Sets the stream-position indicator.

#include <stdio.h>

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

stream Target stream  
pos Position-indicator storage  

Remarks

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 the ungetc function on stream. After calling fsetpos, the next operation on stream may be either input or output.

Return Value

If successful, the fsetpos function returns 0. On failure, the function returns a nonzero value and sets errno to one of the following manifest constants (defined in ERRNO.H):

Constant Meaning

EBADF The object that stream points to is not a valid file handle, or the file is not accessible.
EINVAL An invalid stream value was passed.

Compatibility

Standards:ANSI

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

fgetpos

Example

/* 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 )

perror( "fsetpos error" );

fread( buffer, sizeof( char ), 10, stream );

printf( "10 bytes at byte %ld: %.10s\n", pos, buffer );

fclose( stream );

}

}

Output

10 bytes at byte 10: .C: This p

10 bytes at byte 140: FILE *