Version 8 of seek

Updated 2003-07-08 19:38:29

http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/seek.htm

Example (no error handling done here - be sure to do some in real life!) :

 % cat /tmp/mydata
 1234567890abcdefghijklmnopqrstuvwxyz
 % set f [open "/tmp/mydata" "r+"]
 file3
 % seek $f 20
 % puts -nonewline $f "KLM"
 % close $f
 % cat /tmp/mydata
 1234567890abcdefghijKLMnopqrstuvwxyz


Is there supposed to be a difference in the seek/puts behavior based on varying the open access value between "r+, "w+" and "a+"?


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

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

...

  }

Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command