Ruchi
Ruchi

Reputation: 693

reading a buffer stream in tcl

This is a question regarding file reading in tcl. I am opening a buffer stream to write and i have only one file handler referece to it Now while reading this buffer line by line, at some condition I have to put all the content of the buffer, Please suggest how can i achieve this. So i am just pasting an example code to explain my requirement.

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

Thanks Ruchi

Upvotes: 2

Views: 893

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137807

To read from the pipe until it is closed by the other end, just use read $pipe. That then lets you do this:

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]
    }
}

If you want anything from earlier in the piped output, you must save it in a variable.

Upvotes: 4

Related Questions