title: read system call doesn't detect end of file
body: <p>I'm trying to create a function that reads an entire file using a specific read size that can change anytime, but the read system call doesn't store the characters properly in the buffer, so far I'm only trying to print until the end of file like this: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  
  # define READ_SIZE (42)
  
  int main(int argc, char **argv)
   {
     int fd;
     int rd;
     char *buffer;
  
     buffer = malloc(READ_SIZE);
     fd = open(argv[1], O_RDONLY);
     while ((rd = read(fd, buffer, READ_SIZE)) &gt; 0)
      {
         printf("%s", buffer);
      }
      return (0);
   }
  </code></pre>
  
  <p>This is the file that I'm trying to read:</p>
  
  <pre><code>test1234
  test123
  test1
  test2
  test3
  test4
  test
  </code></pre>
  
  <p>This is the output of my program:</p>
  
  <pre><code>test123
  test12
  test1
  test2
  test3
  test4
  testest123
  test12
  test1
  test2
  test3
  test4
  tes
  </code></pre>
  
  <p>I can only use <code>malloc</code>, and read to handle this, open is only for testing, and I don't understand why it does this, usually read returns the number of bytes read in that file, and 0 if it reaches the end of file, so it's a bit weird to see this.</p>