haiv icon

Write to the middle of a file

haiv | PRO | 11/06/12 07:10:52 AM UTC | 0 ⭐ | 405 👁️ | Never ⏰ | []
TCL |

420 B

|

None

|

0 👍

/

0 👎

#!/usr/bin/env tclsh
 
# Open the file, with truncation
set f [open foo w]
puts $f "one"
puts $f "two"
close $f
 
# Open again, with a+ ==> read/write/append
set f [open foo a+]
puts $f "three" ;# This goes to the end of the file
seek $f 4       ;# Seek to the beginning of the word "two"
puts $f "2.0"   ;# Overwrite the word "two"
close $f
 
# Open and verify the contents
set f [open foo r]
puts [read $f]
close $f

Comments