这是一个关于tcl中文件读取的问题。 我正在打开一个缓冲区流进行写入,并且只有一个文件处理程序引用它 现在,在逐行读取此缓冲区时,在某些情况下我必须放入缓冲区的所有内容,请建议我如何实现这一点。 所以我只是粘贴示例代码来解释我的要求。
catch { open "| grep" r } pipe
while { [gets $pipe line] } {
if { some_condition } {
## display all the content of $pipe as string
}
}
谢谢 鲁奇
最佳答案
要从管道中读取数据直到另一端将其关闭,只需使用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]
}
}
如果您想要管道输出中早期的任何内容,则必须将其保存在变量中。
关于tcl - 读取tcl中的缓冲流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12424622/