title: read() only reads a few bytes from file
body: <p>I wanted to read the content of a file using the read() function. I tried the following:</p>
  
  <pre><code>#define BUFFER_LENGTH (1024)
  
  char buffer[BUFFER_LENGTH];
  
  // The first version of the question had a typo:
  // void read_file(const char filename)
  // This would produce a compiler warning.
  void read_file(const char *filename)
  {
      ssize_t read_bytes = 0;
  
      // The first version had the mode in hex instead of octal.
      //
      //     int fd_in = open(filename, O_RDONLY, 0x00644);
      //
      // This does not cause problems here but it is wrong.
      // The mode is now octal (even if it is not needed).
      int fd_in = open(filename, O_RDONLY, 0644);
      if (fd_in == -1)
      {
          return;
      }
  
      do
      {
          read_bytes = read(fd_in, buffer, (size_t) BUFFER_LENGTH);
          printf("Read %d bytes\n", read_bytes);
  
          // End of file or error.
          if (read_bytes &lt;= 0)
          {
              break;
          }
      } while (1);
  
      close(fd_in);
  }
  </code></pre>
  
  <p>I am using 'gcc (GCC) 3.4.2 (mingw-special)' on a Windows 7 system.</p>
  
  <p>The strange behaviour I get is that not all the content is read. For example, I have
  a file</p>
  
  <pre><code>05.01.2012  12:28            15.838 hello.exe
  </code></pre>
  
  <p>and when I try to read it I get:</p>
  
  <pre><code>Read 216 bytes
  Read 0 bytes
  </code></pre>
  
  <p>As far as I know read() should keep reading until it reaches the end of the file. While does
  it report an end of file (0) the second time it is called?</p>
  
  <p>Maybe I am missing something obvious but I cannot see it. I have read <a href="http://www.gnu.org/software/libc/manual/html_node/Opening-and-Closing-Files.html#Opening-and-Closing-Files" rel="nofollow">this document</a> and <a href="http://www.gnu.org/software/libc/manual/html_node/I_002fO-Primitives.html#I_002fO-Primitives" rel="nofollow">this document</a> over and over again and I cannot find what I am doing wrong. Does anyone have any clue?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Thanks for the hint! It is a typo in the question (I have corrected it). It is correct in the source
  code.</p>