Version 5 of Additional file commands

Updated 2001-12-18 16:03:21

Read a text file into a string:

 proc readfile filename {
    set fp [open $filename]
    set res [read $fp [file size $filename]]
    close $fp
    set res 
 }

Just out of curiosity... would the minimal code below be equivalent, i.e. is the close indeed automatic in this context? -JCW

 proc readfile filename {
    read [open $filename] [file size $filename]
 }

RS: According to man close, "Channels are automatically closed when an interpreter is destroyed and when the process exits." From this I imply they are not closed automatically elsewhere. A handle of a file open for reading can't do much harm, except leak some memory (and may cause the app to run out of available file descriptors...) Anyway, I've applied the simple rule "if you opened it, close it" ;-)


Touch a file: without modifying the contents, change a file's access time to (now); create it if it doesn't exist, with 0 bytes contents:

 proc touch filename {
    close [open $filename a]
 } ;# after Don Porter in c.l.t, brought here by RS 

Why not just use

        file attributes mtime [[someone want to fill in the clock command here?]]

RS file attributes handles platform specific flags, like group/owner/permission; archive/hidden. Maybe in very new versions file mtime can be used with a second argument to set the mtime; but not in 8.23.


file - Arts and crafts of Tcl-Tk programming