FSEEK(3C) | Standard C Library Functions | FSEEK(3C) |
fseek, fseeko - reposition a file-position indicator in a stream
#include <stdio.h> int fseek(FILE *stream, long offset, int whence);
int fseeko(FILE *stream, off_t offset, int whence);
The fseek() function sets the file-position indicator for the stream pointed to by stream. The fseeko() function is identical to fseek() except for the type of offset.
The new position, measured in bytes from the beginning of the file, is obtained by adding offset to the position specified by whence, whose values are defined in <stdio.h> as follows:
SEEK_SET
SEEK_CUR
SEEK_END
If the stream is to be used with wide character input/output functions, offset must either be 0 or a value returned by an earlier call to ftell(3C) on the same stream and whence must be SEEK_SET. This constraint does not hold for streams created by open_wmemstream(3C).
A successful call to fseek() clears the end-of-file indicator for the stream and undoes any effects of ungetc(3C) and ungetwc(3C) on the same stream. After an fseek() call, the next operation on an update stream may be either input or output.
If the most recent operation, other than ftell(3C), on a given stream is fflush(3C), the file offset in the underlying open file description will be adjusted to reflect the location specified by fseek().
The fseek() function allows the file-position indicator to be set beyond the end of existing data in the file. If data is later written at this point, subsequent reads of data in the gap will return bytes with the value 0 until data is actually written into the gap.
The value of the file offset returned by fseek() on devices which are incapable of seeking is undefined.
If the stream is writable and buffered data had not been written to the underlying file, fseek() will cause the unwritten data to be written to the file and mark the st_ctime and st_mtime fields of the file for update.
When using a stream based on open_wmemstream(3C), the fseek() and fseeko() functions no longer operate in terms of bytes. Instead, like the stream itself, the values used in offset are measured in units of wide characters, the underlying data unit of the stream. The values returned by ftell(3C) or ftello(3C) are also in these same units, allowing them to be used in the same way. These streams do not have the constraints of other wide character streams and may the full range of values in whence and offset, the same as would be done for a normal byte-oriented stream.
The fseek() and fseeko() functions return 0 on success; otherwise, they returned −1 and set errno to indicate the error.
The fseek() and fseeko() functions will fail if, either the stream is unbuffered or the stream's buffer needed to be flushed, and the call to fseek() or fseeko() causes an underlying lseek(2) or write(2) to be invoked:
EAGAIN
EBADF
EFBIG
EINTR
EINVAL
EIO
ENOSPC
ENXIO
EPIPE
EPIPE
The fseek() function will fail if:
EOVERFLOW
The fseeko() function will fail if:
EOVERFLOW
Although on the UNIX system an offset returned by ftell() or ftello() (see ftell(3C)) is measured in bytes, and it is permissible to seek to positions relative to that offset, portability to non-UNIX systems requires that an offset be used by fseek() directly. Arithmetic may not meaningfully be performed on such an offset, which is not necessarily measured in bytes.
The fseeko() function has a transitional interface for 64-bit file offsets. See lf64(7).
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Standard |
MT-Level | MT-Safe |
getrlimit(2), ulimit(2), ftell(3C), rewind(3C), ungetc(3C), ungetwc(3C), attributes(7), lf64(7), standards(7)
March 25, 2020 | OmniOS |