How can I redirect stdout into a file in tcl

前端 未结 7 1462
不思量自难忘°
不思量自难忘° 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 16:55

    At present when you type foo > bar Tcl is trying to run a foo proc that takes 2 parameter, as this doesn't exist you get the error message. I can think of two ways you could tackle this problem.

    You can redirect at the top level, so when you run tclsh tclsh > bar then all of the output will be redirected, however I doubt this is what you want.

    You could change foo so that it accepts an open file as a parameter and write to that:

    proc foo {fp} {
       puts $fp "some text"
    }
    
    set fp [open bar w]
    foo $fp
    close $fp
    

提交回复
热议问题