title: How to pipe data to a program which calls scanf() and read() in Linux
body: <p>I have a C program which looks like this:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int main()
  {
      int n;
      char str[16];
      scanf("%d", &amp;n);
      printf("n: %d\n", n);
  
      int count = read(STDIN_FILENO, str, 16);
      printf("str: %s\n", str);
      printf("read %d bytes\n", count);
  }
  </code></pre>
  
  <p>If I pipe data into this program using a command like</p>
  
  <pre>(echo -en '45\n'; echo -en 'text\n') | ./program</pre>
  
  <p>only <code>scanf()</code> actually reads data. <code>read()</code> simply reads 0 bytes.</p>
  
  <p>In other words, the program outputs</p>
  
  <pre>n: 45
  str:
  read 0 bytes</pre>
  
  <p>How can I pipe data to both <code>scanf()</code> and <code>read()</code>?</p>
  
  <p><strong>Edit:</strong> Sorry, I should have clarified: I'm looking for a way to do this without modifying the source. Thanks to the people who answered and commented anyway.</p>