在tcl中读取缓冲流

时间:2012-09-14 12:30:51

标签: tcl file-handling

这是关于tcl中文件读取的问题。 我打开一个缓冲流来写,我只有一个文件处理程序参考它 现在,当逐行读取此缓冲区时,在某些情况下我必须放入缓冲区的所有内容,请建议我如何实现此目的。 所以我只是粘贴一个示例代码来解释我的要求。

catch { open "| grep" r } pipe
while { [gets $pipe line] } {
      if { some_condition } {
          ## display all the content of $pipe as string
      }
}

由于 Ruchi

1 个答案:

答案 0 :(得分:4)

要从管道读取直到它被另一端关闭,只需使用read $pipe。然后,你可以这样做:

set pipe [open "| grep" r]
while { [gets $pipe line] >= 0 } {  # zero is an empty line...
    if { some_condition } {
        puts [read $pipe]
        ### Or, to include the current line:
        # puts $line\n[read $pipe]
    }
}

如果您想要管道输出中较早的内容,则必须将其保存在变量中。