title: File splitter in C++
body: <p>I'm trying to make a file splitter/joiner in C++ and I have trouble with my split function. I used an MP4 file for a quick test and the result was: every other parts are OK but the last part always doesn't work. I don't understand this. Can anyone please explain it to me? Here is my split function:</p>
  
  <pre><code>void split_F(const char* file_name, int number_of_part)
  {
      FILE *fp_read = fopen(file_name, "rb");
  
      //calculate file size
      int file_size;
      fseek(fp_read, 0L, SEEK_END);
      file_size = ftell(fp_read);
      rewind(fp_read); //reset file pointer
  
      //calculate number of parts
      //int number_of_part = (int)ceil((double)file_size / size_of_part);
      long size_of_part;
      size_of_part = (int)floor((double)file_size / number_of_part);
      cout &lt;&lt; "Total files after split: " &lt;&lt; number_of_part &lt;&lt; endl
          &lt;&lt; "...Processing..." &lt;&lt; endl;
  
      //main process
      char name[255] = "";
      for (int count = 1; count &lt;= number_of_part; count++) 
      {
          sprintf(name, "%s.part_%03d", file_name, count);
          FILE *fp_write = fopen(name, "wb");
  
          //create buffer
          char *buffer = new char[size_of_part];
          memset(buffer, NULL, size_of_part); //reset buffer
  
          fread(buffer, size_of_part, 1, fp_read);
          fwrite(buffer, size_of_part, 1, fp_write);
          fseek(fp_read, count*size_of_part, SEEK_SET);
  
          cout &lt;&lt; "&gt; File: " &lt;&lt; name &lt;&lt; " done babe!" &lt;&lt; endl;
  
          delete[] buffer;
          fclose(fp_write);
      }
          fclose(fp_read);
  }
  </code></pre>