Copying a file

suggest change
std::ifstream  src("source_filename", std::ios::binary);
std::ofstream  dst("dest_filename",   std::ios::binary);
dst << src.rdbuf();

If either open fails, the stream sets failbit and this does not throw unless exceptions have been enabled. With C++17 the standard way to copy a file is to include the <filesystem> header and use copy_file:

std::filesystem::copy_file("source_filename", "dest_filename");

This overload throws std::filesystem::filesystem_error if the destination already exists. The ofstream example truncates an existing destination instead.

The filesystem library was originally developed as boost.filesystem and finally merged to ISO C++ as of C++17.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents