ZetCode

Tcl tell Command

last modified April 3, 2025

The Tcl tell command returns the current access position in an open file. It's used with file channels to determine where the next read or write operation will occur.

Basic Definition

The tell command reports the current position in a file channel as a byte offset from the beginning. It works with both read and write operations.

Syntax: tell channelId. The command takes one argument - the file channel identifier returned by open or socket.

Basic File Position Check

This example shows how to get the current position in a file after opening it.

basic_tell.tcl
set file [open "data.txt" r]
set position [tell $file]
puts "Initial position: $position"
close $file

This opens a file for reading and immediately checks the position, which will be 0 at the start. The position is stored in a variable and printed.

Tracking Position While Reading

This demonstrates how the file position changes as we read data from a file.

tell_reading.tcl
set file [open "data.txt" r]
set chunk [read $file 10]
set pos1 [tell $file]
set chunk [read $file 20]
set pos2 [tell $file]
puts "Position after first read: $pos1"
puts "Position after second read: $pos2"
close $file

After each read operation, we check the position. The position increases by the number of bytes read. This helps track progress through the file.

Combining tell with seek

This shows how tell can be used with seek to navigate a file.

tell_seek.tcl
set file [open "data.txt" r+]
seek $file 50 start
set pos [tell $file]
puts "Current position: $pos"
seek $file 10 current
set new_pos [tell $file]
puts "New position: $new_pos"
close $file

We first seek to position 50, verify with tell, then seek 10 bytes forward from current position. The tell command confirms each move.

Writing and Position Tracking

This example demonstrates position tracking during write operations.

tell_writing.tcl
set file [open "output.txt" w]
puts $file "First line"
set pos1 [tell $file]
puts $file "Second line"
set pos2 [tell $file]
puts "After first write: $pos1"
puts "After second write: $pos2"
close $file

Each write operation advances the file position. The tell command lets us track how much data has been written and where new data will be added.

Binary File Position Handling

This shows tell working with binary files where byte positions are critical.

tell_binary.tcl
set file [open "image.png" rb]
seek $file 16
set header_pos [tell $file]
set header [read $file 8]
set data_pos [tell $file]
puts "Header at $header_pos, data starts at $data_pos"
close $file

In binary files, exact positions matter. We seek to a known offset, read a header, and use tell to find where the actual data begins.

Error Handling with tell

This example demonstrates proper error handling when using tell.

tell_error.tcl
if {[catch {
    set file [open "nonexistent.txt" r]
    set pos [tell $file]
    close $file
} err]} {
    puts "Error: $err"
}

The catch command handles potential errors when working with files. This prevents crashes if the file doesn't exist or if the channel is invalid.

Best Practices

This tutorial covered the Tcl tell 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.