tell

tell - Return current access position for an open channel
https://www.tcl-lang.org/man/tcl8.6/TclCmd/tell.htm

tell channelId

Returns an integer string giving the current access position in channelId. This value returned is a byte offset that can be passed to seek in order to set the channel to a particular position. Note that this value is in terms of bytes, not characters like read. The value returned is -1 for channels that do not support seeking.

ChannelId must be an identifier for an open channel such as a Tcl standard channel (stdin, stdout, or stderr), the return value from an invocation of open or socket, or the result of a channel creation command provided by a Tcl extension.

tell and flush

If you are using tell and you do reads and writes in between your calls to tell, you usually will have to do a flush or your tell will fail with a -1 return value.

This is because the Tcl Channel code cannot ensure an accurate 'tell' value until you flush out pending i/o on your open channel. -mikeH

Clif was nice enough to type an example of this -1 return value behaviour for tell in the Tcl'ers chat. It's included below:

 % set f [open tst.dat w+]
 file4
 % tell $f
 0
 % puts $f "a"
 % tell $f
 2
 % seek $f 0
 % read $f 1
 a
 % tell $f
 1
 % puts $f bb
 % tell $f
 -1

tell and open using "a", "a+", APPEND , etc..

If you are using tell in combination with a channel opened via APPEND (et. al), keep in mind that for each write performed, your file position is going to be placed at the end of the file. Tcl Channels are reflecting C library behaviour in this respect (and rightly so if you think about it for a bit). -mikeH

need some examples of this behaviour here...


One interesting use for tell is to constrain the size of output files. One might have

    proc constrained_log {log_fp message} {
        set current_size [tell $log_fp]
        if {expr {current_size + [string length $message] > $::log_limit}} {
            error "It's gotten too big ..."
        } else {
            puts $log_fp $message
        }
     ...
    }


See also