title: How do I read from stdin a text and write it to a file using low level file processing?
body: <p>I tried using the following code to read a text from the keyboard and write it into the file <em>text.dat</em>. The file was created but it was empty.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;io.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
      char s[201];
      int n,f = open("text.dat", O_RDWR | O_CREAT);
      while (fgets(s,200,stdin) != NULL)
          write(f, s,sizeof(s));
      close(f);
      return 0;
  }
  </code></pre>