【发布时间】:2011-08-03 00:35:27
【问题描述】:
在 TCL 中,如何使用for 循环或foreach 循环将不同的内容附加到单个文件中?
【问题讨论】:
-
“不同的内容”是什么意思?
在 TCL 中,如何使用for 循环或foreach 循环将不同的内容附加到单个文件中?
【问题讨论】:
你的意思是这样的吗?
set fo [open file a]
foreach different_content {"text 1" "text two" "something else" "some content"} {
puts $fo $different_content
}
close $fo
您以a 模式(追加)打开文件file 并写入文件描述符(示例中为$fo)。
更新:如果要追加变量内容,则必须将脚本更改为:
set fo [open file a]
foreach different_content [list $data1 $data2 $data3 $data4] {
puts $fo $different_content
}
close $fo
【讨论】:
foreach different_content "$data1 $data2" {puts $fo $different_content}
以下代码适用于从文件中读取,其中 sample.tcl 文件位于“p”文件夹中。
标题
set fp [open "p://sample.tcl" r]
set file_data [read $fp]
puts $file_data
close $fp
【讨论】:
以下代码在文件夹'P' 中创建一个文本文件并写入其中。
set fid [open p:\\temp.txt w]
puts $fid "here is the first line."
close $fid
【讨论】: