tcl - 读取tcl中的缓冲流

标签 tcl file-handling

这是一个关于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/

相关文章:

php - 无法使用PHP将数据从excel上传到mysql

string - TCL分割字符串

tcl - 如何在 TCL 中的 http get 查询中获取响应

c - C 文件处理中的错误

C程序不读取文件

python - 将稀疏矩阵转储到文件中

c - C 中的文件处理 - 从文本文件列表中删除特定单词

tcl - 防止 Tcl 在未知命令上崩溃

tcl - 将值存储到 TCL 中返回函数的变量中

tcl - 如何在 tcl switch 语句中使用 或 ?