Ken
Ken

Reputation: 173

How to write to file without opening it

Is there anyway in TCL to create a file and write to it, without opening and closing it? This is how I'm doing it right now:

set resultFile [open "$logFile.log" w]  

puts $resultFile $data

close $resultFile

I'm asking this because I have to perform this operation numerous times and it will help in runtime if I can just pipe the data to the file directly.

Upvotes: 1

Views: 1158

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385800

The mechanism for writing to a file requires that it be open. You are correct that repeatedly opening a file can be slow when done in a loop (though, the loop has to be pretty big to notice it).

You don't have to open it before each call to puts, however. You can open it once at the start of your program and keep it open while the program runs.

Upvotes: 3

Related Questions