Copying a file
suggest changestd::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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents