6.12.7 Random Access

Scheme Procedure: seek fd_port offset whence
C Function: scm_seek (fd_port, offset, whence)

Sets the current position of fd_port to the integer offset. For a file port, offset is expressed as a number of bytes; for other types of ports, such as string ports, offset is an abstract representation of the position within the port’s data, not necessarily expressed as a number of bytes. offset is interpreted according to the value of whence.

One of the following variables should be supplied for whence:

Variable: SEEK_SET

Seek from the beginning of the file.

Variable: SEEK_CUR

Seek from the current position.

Variable: SEEK_END

Seek from the end of the file.

On systems that support it, such as GNU/Linux, the following constants can be used for whence to navigate “holes” in sparse files:

Variable: SEEK_DATA

Seek to the next location in the file greater than or equal to offset containing data. If offset points to data, then the file offset is set to offset.

Variable: SEEK_HOLE

Seek to the next hole in the file greater than or equal to the offset. If offset points into the middle of a hole, then the file offset is set to offset. If there is no hole past offset, then the file offset is adjusted to the end of the file—i.e., there is an implicit hole at the end of any file.

If fd_port is a file descriptor, the underlying system call is lseek (see File Position Primitive in The GNU C Library Reference Manual). port may be a string port.

The value returned is the new position in fd_port. This means that the current position of a port can be obtained using:

(seek port 0 SEEK_CUR)
Scheme Procedure: ftell fd_port
C Function: scm_ftell (fd_port)

Return an integer representing the current position of fd_port, measured from the beginning. Equivalent to:

(seek port 0 SEEK_CUR)
Scheme Procedure: truncate-file file [length]
C Function: scm_truncate_file (file, length)

Truncate file to length bytes. file can be a filename string, a port object, or an integer file descriptor. The return value is unspecified.

For a port or file descriptor length can be omitted, in which case the file is truncated at the current position (per ftell above).

On most systems a file can be extended by giving a length greater than the current size, but this is not mandatory in the POSIX standard.