Reading, writing, and adding a file to TCL

In TCL, how to add other content to one file using a for loop or foreach ?

+7
source share
3 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 the file file in mode a (add) and write it to the file descriptor ( $fo in the example).

Update. If you want to add the contents of a variable, you need to change the script to:

 set fo [open file a] foreach different_content [list $data1 $data2 $data3 $data4] { puts $fo $different_content } close $fo 
+19
source

The following code is suitable for reading from a file where the sample.tcl file is available in the "p" folder.

Headline

 set fp [open "p://sample.tcl" r] set file_data [read $fp] puts $file_data close $fp 
0
source

The following code creates a text file in the 'P' folder and writes to it.

 set fid [open p:\\temp.txt w] puts $fid "here is the first line." close $fid 
-one
source

All Articles