fseek
reposition a stream
You're viewing an older version of this page (#3273). View the current version.
NAME
<span class="Nm" id="fgetpos">fgetpos</span>, <span class="Nm" id="fseek">fseek</span>, <span class="Nm" id="fseeko">fseeko</span>, <span class="Nm" id="fsetpos">fsetpos</span>, <span class="Nm" id="ftell">ftell</span>, <span class="Nm" id="ftello">ftello</span>, <span class="Nm" id="rewind">rewind</span>
INTERFACE
#include <stdio.h> int fseek ( FILE *stream, long offset, int whence ) long ftell ( FILE *stream ) void rewind ( FILE *stream ) int fgetpos ( FILE * restrict stream, fpos_t * restrict pos ) int fsetpos ( FILE *stream, const fpos_t *pos ) #include <sys/types.h> int fseeko ( FILE *stream, off_t offset, int whence ) off_t ftello ( FILE *stream )
DESCRIPTION
The fseek function sets the file position indicator for the stream pointed to by \c stream. The new position, measured in bytes, is obtained by adding \c offset bytes to the position specified by \c whence. If \c whence is set to <span class="Dv">SEEK_SET</span>, <span class="Dv">SEEK_CUR</span>, or <span class="Dv">SEEK_END</span>, the offset is relative to the start of the file, the current position indicator, or end-of-file, respectively. A successful call to the fseek function clears the end-of-file indicator for the stream and undoes any effects of the ungetc (3) and reference:ungetwc (3) functions on the same stream. \n \n The ftell function obtains the current value of the file position indicator for the stream pointed to by \c stream. \n \n The rewind function sets the file position indicator for the stream pointed to by \c stream to the beginning of the file. It is equivalent to: \n \n \n
(void)fseek(stream, 0L, SEEK_SET)\n \n \n except that the error indicator for the stream is also cleared (see reference:clearerr (3) ). \n \n Since rewind does not return a value, an application wishing to detect errors should clear <span class="Va">errno</span>, then call rewind , and if <span class="Va">errno</span> is non-zero, assume an error has occurred. \n \n The fseeko function is identical to fseek , except it takes an \c off_t argument instead of a \c long. Likewise, the ftello function is identical to ftell , except it returns an \c off_t. \n \n The fgetpos and fsetpos functions are alternate interfaces for retrieving and setting the current position in the file, similar to ftell and fseek , except that the current position is stored in an opaque object of type <span class="Vt">fpos_t</span> pointed to by \c pos. These functions provide a portable way to seek to offsets larger than those that can be represented by a <span class="Vt">long</span> <span class="Vt">int</span>. They may also store additional state information in the <span class="Vt">fpos_t</span> object to facilitate seeking within files containing multibyte characters with state-dependent encodings. Although <span class="Vt">fpos_t</span> has traditionally been an integral type, applications cannot assume that it is; in particular, they must not perform arithmetic on objects of this type. \n \n If the stream is a wide character stream (see reference:fwide (3) ), the position specified by the combination of \c offset and \c whence must contain the first byte of a multibyte sequence.
RETURN VALUES
The rewind function returns no value. \n \n <span class="Rv">-std</span> <span class="Rv">fgetpos</span> <span class="Rv">fseek</span> <span class="Rv">fseeko</span> <span class="Rv">fsetpos</span> \n \n Upon successful completion, ftell and ftello return the current offset. Otherwise, -1 is returned and the global variable <span class="Va">errno</span> is set to indicate the error.
ERRORS
<table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EBADF</span></span>] </td> <td valign="top"> The \c stream argument is not a seekable stream. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EINVAL</span></span>] </td> <td valign="top"> The \c whence argument is invalid or the resulting file-position indicator would be set to a negative value. </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">EOVERFLOW</span></span>] </td> <td valign="top"> The resulting file offset would be a value which cannot be represented correctly in an object of type \c off_t for fseeko and ftello or \c long for fseek and ftell . </td> </tr> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ESPIPE</span></span>] </td> <td valign="top"> The file descriptor underlying stream is associated with a pipe or FIFO or file-position indicator value is unspecified (see ungetc (3) ). </td> </tr> </table> \n \n The functions fgetpos , fseek , fseeko , fsetpos , ftell , and ftello may also fail and set <span class="Va">errno</span> for any of the errors specified for the routines fflush (3) , reference:fstat (2) , reference:lseek (2) , and malloc (3) .
SEE ALSO
reference:lseek (2) , reference:clearerr (3) , reference:fwide (3) , ungetc (3) , reference:ungetwc (3)
STANDARDS
The fgetpos , fsetpos , fseek , ftell , and rewind functions conform to \n \n The fseeko and ftello functions conform to