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

stream_truncate(+Stream)

Truncate Stream at the current position
Stream
Stream handle or alias (atom)

Description

Used to truncate a stream, i.e. to remove all contents beyond the current (write) position. As a consequence, immediately after this operation the stream is at end of file.

This operation only makes sense on file-streams and string streams which are open for writing (or updating). On all other types of streams, the predicate silently succeeds.

Files will be physically truncated, i.e. their size is trimmed and extra disk space returned to the operating system. String streams will free any unnecessary buffer memory.

Note: When opening an existing file for writing (not updating), the file is automatically truncated to size zero (see open/3,4).

Modes and Determinism

Exceptions

(4) instantiation fault
Stream is not instantiated.
(5) type error
Stream is instantiated, but not to an atom or a stream handle.
(192) illegal stream mode
Stream is not open in write mode.

Examples

        ?- open(string("hellothere"), update, s).
        Yes (0.00s cpu)

        ?- seek(s, 5).
        Yes (0.00s cpu)

        ?- at_eof(s).
        No (0.00s cpu)

        ?- stream_truncate(s).
        Yes (0.00s cpu)

        ?- at_eof(s).
        Yes (0.00s cpu)

        ?- seek(s,0).
        Yes (0.00s cpu)

        ?- read_string(s, end_of_file, _, S).
        S = "hello"
        Yes (0.00s cpu)

See Also

at_eof / 1, seek / 2, open / 3, open / 4