ZetCode

Tcl seek Command

last modified April 3, 2025

The Tcl seek command is used to change the current position within an open file. It allows random access to file contents rather than just sequential reading or writing.

Basic Definition

The seek command moves the file pointer to a specified position in a file channel. This enables reading or writing at arbitrary locations.

Syntax: seek channelId offset ?origin?. The channelId is the file handle, offset is the position, and origin specifies the reference point.

Basic File Positioning

This example shows how to move to a specific position in a file.

basic_seek.tcl
set file [open "data.txt" r]
seek $file 100
set data [read $file 20]
close $file
puts $data

This opens a file, moves to position 100, reads 20 bytes from that position, then closes the file. The default origin is the start of the file.

Seek from Current Position

The origin can be specified to seek relative to the current position.

seek_current.tcl
set file [open "data.bin" r]
seek $file 50
seek $file 25 current
set data [read $file 10]
close $file
puts $data

This first seeks to position 50, then moves 25 bytes forward from there. The final read starts at position 75 (50 + 25) in the file.

Seek from End of File

You can seek relative to the end of file using the 'end' origin.

seek_end.tcl
set file [open "log.txt" r]
seek $file -100 end
set data [read $file]
close $file
puts $data

This positions the file pointer 100 bytes before the end of file. The read then captures the last 100 bytes of the file.

Seek with Binary Files

Seek is particularly useful with binary files where precise positioning matters.

seek_binary.tcl
set file [open "image.png" rb]
seek $file 16
set header [read $file 8]
close $file
puts "Header chunk: $header"

This opens a binary file, seeks to position 16, and reads 8 bytes of header data. The 'b' in open mode ensures binary reading on Windows.

Seek with Writing

Seek can position the pointer for writing operations as well as reading.

seek_write.tcl
set file [open "data.txt" r+]
seek $file 50
puts -nonewline $file "INSERTION"
close $file

This opens a file for both reading and writing, seeks to position 50, and writes data at that position. The -nonewline prevents adding extra newlines.

Combining Seek and Tell

The tell command can report the current position after seeking.

seek_tell.tcl
set file [open "data.txt" r]
seek $file 150
set pos [tell $file]
set data [read $file 10]
close $file
puts "Read from position $pos: $data"

This seeks to position 150, confirms the position with tell, then reads 10 bytes. The tell command helps verify the file pointer location.

Best Practices

This tutorial covered the Tcl seek command with practical examples showing its usage in different file handling scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.