Skip to content

Instantly share code, notes, and snippets.

@W4RH4WK
Created November 12, 2021 12:21
Show Gist options
  • Save W4RH4WK/1f5647fc440053169495502c3fcc2c1d to your computer and use it in GitHub Desktop.
Save W4RH4WK/1f5647fc440053169495502c3fcc2c1d to your computer and use it in GitHub Desktop.
void copyOver(std::ofstream& dst, std::ifstream& src, std::uint64_t length)
{
// This seems to be broken, we are skipping bytes at some point. Apparently
// istream_iterator shouldn't be used for binary files, despite the
// underlying stream being opened in binary mode. istreambuf_iterator was
// suggested, but is not applicable here as it fills an internal buffer with
// more than we read and therefore advances the file cursor too far.
//
// std::copy_n(std::istream_iterator<uint8_t>(src), length,
// std::ostream_iterator<uint8_t>(dst));
//
// Copying bytes manually instead.
std::array<char, 4096> buffer;
while (length > buffer.size()) {
src.read(buffer.data(), buffer.size());
dst.write(buffer.data(), buffer.size());
length -= buffer.size();
}
src.read(buffer.data(), length);
dst.write(buffer.data(), length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment