File reading, writing and appending in TCL

galvin Verghese picture galvin Verghese · Apr 11, 2011 · Viewed 32.3k times · Source

In TCL, how to append different content into a single file using the for loop or foreach loop?

Answer

bmk picture bmk · Apr 11, 2011

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