How can I redirect stdout into a file in tcl

前端 未结 7 1448
不思量自难忘°
不思量自难忘° 2020-12-11 16:36

how can I redirect a proc output into a file in tcl, for example, I have a proc foo, and would like to redirect the foo output into a file bar. But got this result



        
相关标签:
7条回答
  • 2020-12-11 17:13

    If you cannot change your code to take the name of a channel to write to (the most robust solution), you can use a trick to redirect stdout to a file: reopening.

    proc foo {} { puts "hello world" }
    proc reopenStdout {file} {
        close stdout
        open $file w        ;# The standard channels are special
    }
    
    reopenStdout ./bar
    foo
    reopenStdout /dev/tty   ;# Default destination on Unix; Win equiv is CON:
    

    Be aware that if you do this, you lose track of where your initial stdout was directed to (unless you use TclX's dup to save a copy).

    0 讨论(0)
提交回复
热议问题