title: Fastest way to copy data from one file to another in C/C++?
body: <p>In my code, I have a situation where I need to copy data from one file to another. The solution I came up with looks like this:</p>
  
  <pre><code>const int BUF_SIZE = 1024;
  char buf[BUF_SIZE];
  
  int left_to_copy = toCopy;
  while(left_to_copy &gt; BUF_SIZE)
  {
      fread(buf, BUF_SIZE, 1, fin);
      fwrite(buf, BUF_SIZE, 1, fout);
      left_to_copy -= BUF_SIZE;
  }
  
  fread(buf, left_to_copy, 1, fin);
  fwrite(buf, left_to_copy, 1, fout);
  </code></pre>
  
  <p>My main thought was that there might be something like memcpy, but for data in files. I just give it two file streams and the total number of bytes. I searched a bit but I couldn't find any such thing.</p>
  
  <p>But if something like that isn't available, what buffer size should I use to make the transfer fastest? Bigger would mean fewer system calls, but I figured it could mess up other buffering or caching on the system. Should I dynamically allocate the buffer so it only takes on pair of read/write calls? Typical transfer sizes in this particular case are from a few KB to a dozen or so MB. </p>
  
  <p>EDIT: For OS specific information, we're using Linux.  </p>
  
  <p>EDIT2:</p>
  
  <p>I tried using sendfile, but it didn't work. It seemed to write the right amount of data, but it was garbage.</p>
  
  <p>I replaced my example above with something that looks like this:</p>
  
  <pre><code>fflush(fin);
  fflush(fout);
  off_t offset = ftello64(fin);
  sendfile(fileno(fout), fileno(fin), &amp;offset, toCopy);
  fseeko64(fin, offset, SEEK_SET);
  </code></pre>
  
  <p>I added the flush, offest, and seeking one at a time since it didn't appear to be working. </p>