[ Stream I/O | Reference Manual | Alphabetic Index ]

seek(+Stream, +Offset)

The pointer in stream Stream is offset Offset from the start of the file
Stream
Stream handle or alias (atom)
Offset
Integer or the atom end_of_file.

Description

Moves the file pointer to offset Offset from the start of the file opened. It is an error if Stream is not a stream or if Offset is not an integer or the atom 'end_of_file'.

seek/2 seeks to the end of the file when Offset is instantiated to end_of_file.

Only file and string streams are seekable. seek/2 has no effect on the null stream, it always succeeds.

Modes and Determinism

Exceptions

(4) instantiation fault
Either Stream or Offset is uninstantiated.
(5) type error
Offset is instantiated, but not to an integer or the atom end_of_file.
(5) type error
Stream is instantiated, but not to an atom or a stream handle.
(6) out of range
Offset is a negative integer or greater than the file length.
(192) illegal stream mode
Stream is not seekable.
(193) illegal stream specification
Stream is an illegal stream specification.

Examples

Success:
      seek(0, null). % does not modify, only succeeds

      ?- open(file1,update,S), write(S,hello),
         seek(S,3), read(S,T), close(S).
      T = lo
      yes.

Error:
      seek( Offset,7).      (Error 4).
      seek("7", 2).         (Error 5).
      seek(7, -1).          (Error 6).
      seek( 0,input).       (Error 192).
      seek(-1, 0).          (Error 193). % does not exist

See Also

at / 2, at_eof / 1, set_stream_property / 3