title: C process created with popen doesn't print it's output
body: <p>I have to solve the following problem as homework: create two processes with popen. The parent process will read from stdin lines of at most 30 chars and give the first child the digits from the line and give the second child the letters from the line, which will transform them to uppercase. Both processes will then print these things to the parent's stdout.
  So basically i want something similar to running the "tee" command. Read a line, write some output and repeat until ctrl-d</p>
  
  <p>Here is my attempt:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;ctype.h&gt;
  
  int main() {
      FILE *digits, *letters;
      char string[31];
  
      if ((digits = popen("tee", "w")) == NULL) {
          perror("can't create pipe for digits");
          return 1;
      }
  
      if ((letters = popen("tr a-z A-Z", "w")) == NULL) {
          perror("can't create pipe for letters");
          return 1;
      }
  
      while(!feof(stdin)) {
          fgets(string, 31, stdin);
          for (char* c = string; *c != '\0'; c++)
              if (isdigit(*c))
                  fputc(*c, digits);
              else if (isalpha(*c))
                  fputc(*c, letters);
          fputc('\n', digits);
          fputc('\n', letters);
      }
      pclose(digits);
      pclose(letters);
      return 0;
  }
  </code></pre>
  
  <p>But after I write a line and press enter, the program expects input again, without printing anything. It prints all of the output at the end, after pressing ctrl-d. And I don't understand why. It reads the line correctly, then it feeds the correct characters to each child, followed by a new line. Why don't these processes immediately print their output? Is the actual command ran when pclose is called? Because I couldn't find any information about that. Also, is writing '\n' to a child's stdin the same as pressing enter? And what's even weirder is that the last line from the output of each child is printed twice.</p>
  
  <p>For example, here is how I would like it to work:</p>
  
  <pre>
  (input)
  1a2b3c
  (output)
  123
  ABC
  (input)
  4d5e6f
  (output)
  456
  DEF
  (ctrl-d)
  </pre>
  
  <p>But I get this:</p>
  
  <pre>
  (input)
  1a2b3c
  (input)
  4d5e6f
  (ctrl-d)
  (output)
  123
  456
  456
  ABC
  DEF
  DEF
  </pre>