title: Read file block by block in C
body: <p>I want to copy the contents of file1 to file2 exactly as they are (keeping spaces and newlines). I specifically want to copy these contents one small block of chars at a time(this is a small segment of a larger project so bear with me).</p>
  
  <p>I have attempted the following:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define MAX 5
  
  int main(int argc, char *argv[]) {
      FILE *fin, *fout;
      char buffer[MAX];
      int length;
      char c;
  
      if((fin=fopen(argv[1], "r")) == NULL){
          perror("fopen");
          exit(EXIT_FAILURE);
      }
  
      if((fout=fopen(argv[2], "w")) == NULL){
          perror("fopen");
          exit(EXIT_FAILURE);
      }
  
      while(1){
          length = 0;
          while((c = fgetc(fin)) != EOF &amp;&amp; length &lt; MAX){
              buffer[length++] = (char) c;
          }
          if(length == 0){
              break;
          }
          fprintf(fout, "%s", buffer);
      }
      fclose(fout);
      fclose(fin);
  }
  </code></pre>
  
  <p>However, this causes incorrect output to my file2. Any input would be appreciated.</p>