Reputation: 605
In TCL, how to append different content into a single file using the for
loop or foreach
loop?
Upvotes: 9
Views: 46763
Reputation: 11
The following code is fine for reading from a file, where sample.tcl
file is available in 'p' folder.
Heading
set fp [open "p://sample.tcl" r]
set file_data [read $fp]
puts $file_data
close $fp
Upvotes: 0
Reputation: 11
The following code creates a text file in folder 'P'
and writes into it.
set fid [open p:\\temp.txt w]
puts $fid "here is the first line."
close $fid
Upvotes: 1
Reputation: 14147
Did you mean something like that?
set fo [open file a]
foreach different_content {"text 1" "text two" "something else" "some content"} {
puts $fo $different_content
}
close $fo
You open file file
in mode a
(append) and write to the file descriptor ($fo
in the example).
Update: If you want to append variable contents, you have to change the script to:
set fo [open file a]
foreach different_content [list $data1 $data2 $data3 $data4] {
puts $fo $different_content
}
close $fo
Upvotes: 24