open filename access
#access 包括r, w, a, r+, w+, a+ read -nonewhile $fp
#读取并返回文件剩余的所有内容 puts -nonewhile $fp str
#将字符串str写入到标识符为fp的文件中, 如果不加-nonewhile, 则str末尾会加换行符
set fp1 [open "file1.txt" "w"]
puts $fp1 "Content: first line."
puts $fp1 "0x1002"
puts $fp1 "45671234"
puts -nonewline $fp1 "Content: end line"
close $fp1
下面是另一个例子读取文件,文件一行一行直到结束。
#!/usr/bin/tclsh
set fp [open "input.txt" w+]
puts $fp "test\ntest"
close $fp
set fp [open "input.txt" r]
while { [gets $fp data] >= 0 } {
puts $data
}
close $fp
set y /u/home/tcl/list.tcl
split $y /
=> {} u home tcl list.tcl