Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File reading, writing and appending in TCL

Tags:

file-io

tcl

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

like image 859
galvin Verghese Avatar asked Apr 11 '11 12:04

galvin Verghese


People also ask

What is append in Tcl?

DESCRIPTION. Append all of the value arguments to the current value of variable varName. If varName doesn't exist, it is given a value equal to the concatenation of all the value arguments. This command provides an efficient way to build up long variables incrementally.

How do you open a file in read and write mode in Tcl?

Here's a section of the man page for Tcl's open command: r Open the file for reading only; the file must already exist. This is the default value if access is not specified. r+ Open the file for both reading and writing; the file must already exist.

What are the commands for file handling in Tcl?

Tcl supports file handling with the help of the built in commands open, read, puts, gets, and close.


1 Answers

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
like image 85
bmk Avatar answered Oct 25 '22 10:10

bmk