Possible Duplicate:
Copy a file in an sane, safe and efficient way
I’ve searched for similar topics, but I couldn’t find the answer for large binary files. Considering that I have very large binary files (like ~10 or ~100 GB each), how do I copy them with the following function, using standard C++ (no POSIX functions) :
bool copy(const std::string& oldName, const std::string& newName)
{
/* SOMETHING */
}
EDIT :
Is the following implementation ok ? (adapted from the link in comments)
bool copy(const std::string& oldName, const std::string& newName)
{
bool ok = false;
std::ifstream oldStream(oldName.c_str(), std::ios::binary);
std::ofstream newStream(newName.c_str(), std::ios::binary);
if (oldStream.is_open() && newStream.is_open()) {
newStream << oldStream.rdbuf();
ok = (oldStream.good() && newStream.good());
}
return ok;
}
You may use
std::fopen,std::fread,std::fwrite, andstd::fclose, all of which are part of the standard C++ library (#include <cstdio>, very portable) and won’t mess up binary data as long as you don’t use a"t"specifier tofopen.It will even be reasonably quick if you pick an appropriate buffer size (say 1 mebibyte) that gets you into the realm of sequential I/O performance.