11.3.200 seek/4


seek(+Stream, +Offset, +Method, -NewLocation)

Seeks to an arbitrary position in Stream.


stream_object, must be ground

A valid Prolog stream.

integer, must be nonvar

The offset, in items, to seek relative to the specified Method. Items are bytes for binary streams, characters for text streams.

one of [bof,current,eof], must be nonvar

Where start seeking, one of the following:

Seek from beginning of the file stream.
Seek from current position of the file stream.
Seek from end of the file stream.


The offset from beginning of the file after seeking operation.


Sets the current position of the file stream Stream to a new position according to Offset and Method. If Method is:

the new position is set to Offset items from beginning of the file stream.
the new position is Offset plus the current position of Stream.
the new position is Offset, plus the current size of the stream.

Avoid using this Method. Determining the size of the stream may be expensive or unsupported for some streams.

Positions and offsets are measured in items, bytes for binary streams and characters for text streams. Note that there may not be any simple relationship between the number of characters read and the byte offset of a text file.

After applying this operation on a text stream, the line counts and line position aspects of the stream position of Stream will be undefined.

The term “file” above is used even though the stream may be connected to other seekable objects that are not files, e.g. an in-memory buffer.


Stream errors (see ref-iou-sfh-est), plus:

Offset or Method is not instantiated.
Stream is not a stream object, or Offset is not an integer, or Method is not an atom.
Method is not one of bof, current or eof, or the resulting position would refer to an unsupported location. Some streams supports setting the position past the current end of the stream, in this case the stream is padded with zero bytes or characters as soon as an item is written to the new location.
Seeking was not possible. Common reasons include: the stream has not been opened with reposition(true), the stream is a text stream that does not implement seeking, or an I/O error happened during seek.

See Also

stream_position/2, set_stream_position/2, open/[3,4], byte_count/2, character_count/2, line_count/2, line_position/2, ref-iou-sfh.

Send feedback on this subject.