title: Merge n files using a C program
body: <p>I am writing a download Accelerator. So I download a file from the webserver into n parts. Now I want to merge the files into 1 single file. So I use the following code. And the file names are in the correct order. But the output file I am getting is different from the original download file. Can you tell me where could the error be ?C</p>
  
  <pre><code>int cbd_merge_files(const char** filenames, int n, const char* final_filename) {
  
    FILE* fp = fopen(final_filename, "wb");
  
    if (fp == NULL) return 1;
  
    char buffer[4097];
  
    for (int i = 0; i &lt; n; ++i) {
  
      const char* fname = filenames[i];
  
      FILE* fp_read = fopen(fname, "rb");
  
      if (fp_read == NULL) return 1;
  
      int n;
  
      while ((n = fread(buffer, sizeof(char), 4096, fp_read))) {
  
          int k = fwrite(buffer, sizeof(char), n, fp);
  
          if (!k) return 1;
      }
      fclose(fp_read);
  
   }
  
   fclose(fp);
  
   return 0;
  }
  </code></pre>
  
  <p>Adding a few more details. The error is not in partitioning, since I am able to do "cat *.part > final.dmg" and the final.dmg works and is same as the original.</p>