title: Fflush does not write buffered text
body: <p>I'm trying to print a 2D table to my terminal using this code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;termios.h&gt;
  #include &lt;sys/wait.h&gt;
  
  char getch()
  {
      char ch;
      struct termios old, new;
      tcgetattr(0, &amp;old); // grab old terminal i/o settings
      new = old; // new settings = old settings
      new.c_lflag &amp;= ~ICANON; // disable buffered i/o
      new.c_lflag &amp;= ~ECHO; //set echo mode
      tcsetattr(0, TCSANOW, &amp;new); // use these new terminal i/o settings now
      ch = getchar();
      tcsetattr(0, TCSANOW, &amp;old); //restore old settings
      return ch;
  }
  
  void readPBM(char *output)
  {
      char tmp[1024];
  
      int fd[2] = {0,0};
      int pid;
  
      //Open the pipe for inter-process communication
      pipe(&amp;fd[0]);
  
      //Fork and test if we are child or parent process
      pid = fork();
      if(pid) //Parent process
      {
          wait(NULL); //Wait for child's end
          close(fd[1]);//Close pipe's write stream
          close(0);//Close stdin
          dup(fd[0]);//Duplicate stdout
          close(fd[0]);//Close old stdout
  
          strcpy(output, "");// Init output at 0
          while(fgets(tmp, 1024, stdin) != NULL) //Put remaining entry in output
          {
              strcat(output, tmp);
          }
      }
      else if(pid == 0) //Child process
      {
          close(fd[0]);//Close pipe's read stream
          close(1);//Close stdout
          dup(fd[1]);//Duplicate stdin
          close(fd[1]);//Close old stdin
  
          printf("A random string ...\n");
      }
      else //Print error if fork failed
      {
          printf("Error creating a new process");
          exit(EXIT_FAILURE);
      }
  }
  
  int main(int argc, char *argv[])
  {
      int i, j;
  
      char *str = NULL;
      char c;
  
      str = malloc(512 * sizeof(char*));
  
      readPBM(str);
  
      printf("%s", str);
      fflush(stdout);
      c = getch();
  }
  </code></pre>
  
  <p>I have a <a href="https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux">UNIX implementation of getch()</a>.</p>
  
  <p>My problem is that my program is waiting for an input to print the table. I tried to <code>fflush(stdout)</code> and to disable terminal buffering with ICANON but it's still not working.</p>
  
  <p>PS: it's not working neither with getchar, scanf, ...</p>
  
  <p>EDIT: So I came up to this minimal example; it seems to be pipe-related.</p>