title: How to read stdin then keyboard input in C?
body: <p>I'm trying to read content redirected/piped to my program (e.g. program &lt; test.txt), then display a command prompt and read keyboard input.</p>
  <p>The piped input prints correctly, then raises an EOF in stdin. After that, I can't seem to get keyboard input. For example, the following code loops infinitely: the character it prints is -1 (EOF).</p>
  <p>What would you suggest to get the keyboard input working?</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int main()
  {
      int ch;
  
      while (EOF != (ch = getchar())) {
          printf(&quot;%c&quot;, ch);
      }
  
      sleep(3);
  
      while (1) {
          while ('\n' != (ch = getchar())) {
              printf(&quot;%c&quot;, ch);
          }
      }
  
      return 0;
  }
  </code></pre>