Home

fsetpos

fsetpos is a function in the C standard I/O library that sets the file position indicator for a stream to a position previously obtained by fgetpos. It is declared in stdio.h and has the signature int fsetpos(FILE *stream, const fpos_t *pos).

The pos argument must refer to a position previously obtained from fgetpos for the same stream. The

Return value is 0 on success and nonzero on error. Possible failure conditions include an invalid stream,

Relation to fseek and ftell: fsetpos and fgetpos are designed to handle nontrivial stream state beyond a

Usage notes and examples: A typical pattern is to save the current position with fgetpos, perform other

Portability: fsetpos is broadly supported in ISO C implementations and in C++ standard libraries that expose

fpos_t
type
may
store
a
complex
representation
of
the
stream
position,
which
can
include
buffering
or
multibyte
conversion
state.
Because
of
this,
fsetpos
can
restore
a
position
more
reliably
on
buffered
or
wide-character
streams
than
a
simple
byte
offset.
a
pos
value
that
does
not
correspond
to
the
stream,
or
an
internal
error
during
restoration
of
the
stream
state.
basic
numeric
offset,
making
them
suitable
for
complex
buffered
streams.
For
simple
seeking
to
byte
offsets,
fseek
and
ftell
are
often
more
straightforward
and
portable.
operations
that
may
move
the
pointer,
and
then
restore
the
position
with
fsetpos.
For
example,
fgetpos(f,
&pos);
/*
perform
reads
or
writes
that
may
advance
the
pointer
*/
fsetpos(f,
&pos);
This
approach
relies
on
using
the
same
stream
and
checking
return
values
for
error
handling.
stdio
functionality.
Ensure
proper
error
checking
and
avoid
assuming
the
saved
position
remains
valid
across
stream
closures
or
major
state
changes.