title: Getting the output of Tcl Interpreter
body: <p>I am trying to get the output of Tcl Interpreter as described in answer of this question <a href="https://stackoverflow.com/questions/16063303/tcl-c-api-redirect-stdout-of-embedded-tcl-interp-to-a-file-without-affecting-th">Tcl C API: redirect stdout of embedded Tcl interp to a file without affecting the whole program</a>. Instead of writing the data to file I need to get it using pipe. I changed <code>Tcl_OpenFileChannel</code> to  <code>Tcl_MakeFileChannel</code> and passed write-end of pipe to it. Then I called <code>Tcl_Eval</code> with some puts. No data came at read-end of the pipe.</p>
  
  <pre><code>#include &lt;sys/wait.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;string.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;tcl.h&gt;
  #include &lt;iostream&gt;
  
  int main() {
      int pfd[2];
      if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
  /*
          int saved_flags = fcntl(pfd[0], F_GETFL);
          fcntl(pfd[0], F_SETFL, saved_flags | O_NONBLOCK);
  */
  
          Tcl_Interp *interp = Tcl_CreateInterp(); 
          Tcl_Channel chan;
          int rc;
          int fd;
  
          /* Get the channel bound to stdout.
           * Initialize the standard channels as a byproduct
           * if this wasn't already done. */
          chan = Tcl_GetChannel(interp, "stdout", NULL);
          if (chan == NULL) {
                  return TCL_ERROR;
          }
  
          /* Duplicate the descriptor used for stdout. */
          fd = dup(1);
          if (fd == -1) {
                  perror("Failed to duplicate stdout");
                  return TCL_ERROR;
          }
  
          /* Close stdout channel.
           * As a byproduct, this closes the FD 1, we've just cloned. */
          rc = Tcl_UnregisterChannel(interp, chan);
          if (rc != TCL_OK)
                  return rc;
  
          /* Duplicate our saved stdout descriptor back.
           * dup() semantics are such that if it doesn't fail,
           * we get FD 1 back. */
          rc = dup(fd);
          if (rc == -1) {
                  perror("Failed to reopen stdout");
                  return TCL_ERROR;
          }
  
          /* Get rid of the cloned FD. */
          rc = close(fd);
          if (rc == -1) {
                  perror("Failed to close the cloned FD");
                  return TCL_ERROR;
          }
  
          chan = Tcl_MakeFileChannel((void*)pfd[1], TCL_WRITABLE | TCL_READABLE);
          if (chan == NULL)
                  return TCL_ERROR;
  
          /* Since stdout channel does not exist in the interp,
           * this call will make our file channel the new stdout. */
          Tcl_RegisterChannel(interp, chan);
  
  
  
          rc = Tcl_Eval(interp, "puts test");
          if (rc != TCL_OK) {
                  fputs("Failed to eval", stderr);
                  return 2;
          }
  
          char buf;
          while (read(pfd[0], &amp;buf, 1) &gt; 0) {
              std::cout &lt;&lt; buf;
          }
  
  }
  </code></pre>