In Tcl 8.5, the behavior of Tcl on Unix was changed so that the O_APPEND
flag is passed to the open()
system call. This causes the OS to always append the data to the file, and is inherited when the FD is passed to subprocesses; for logs, it is exactly the right thing. (In 8.4 and before, and in all versions on Windows, the behavior is simulated inside Tcl's file channel implementation, which will internally seek()
to the end immediately before the write()
; that obviously is subject to potential problems with race conditions when there are multiple processes logging to the same file and is definitely unsafe when the FD is passed to subprocesses.) You can manage truncation of the opened file with chan truncate
(new in 8.5), which works just fine on a+
-opened files.
If you do not want the seek-to-end behavior, you should not use a+
(or a
). Try r+
or some combination of flags, like this:
set f [open $filename {RDWR CREAT}]
For comparison, the a+
option is now exactly the same as the flags RDWR CREAT APPEND
, and not all combinations of longer flags can be described by short form flag specifiers. If you're not specifying APPEND
, you'll need to do the seek $f 0 end
yourself (and watch out for problems with multiple processes if you're appending to logs; that's when APPEND
becomes required and exceptionally hard to correctly simulate any other way).