std::experi...::copy_file(3)
πΈ NAME πΈ
std::experimental::filesystem::copy_file β bestie, it's literally the function that copies one single file, and yes the page title is the function name twice because that's just how this manpage rolls π
(Heads up: the manual name up top is std::experi...::copy_file(3), from the C++ Standard Libary β and yeah, that's how they spelled "Library" up in the header, we don't judge, we celebrate β¨)
β¨ Synopsis β¨
Defined in header <experimental/filesystem>
bool copy_file( const path& from, const path& to );
bool copy_file( const path& from, const path& to, error_code& ec ); (1) (filesystem TS)
bool copy_file( const path& from, const path& to, copy_options options ); (2) (filesystem TS)
bool copy_file( const path& from, const path& to, copy_options options, error_code& ec );
So here's the tea, split into numbered lore drops:
- This is the default one, and it's equivalent to (2) with
copy_options::noneused asoptions. Zero options, zero drama π«Ά - This one copies a single file from
fromtoto, using the copy options indicated byoptions. And okay, pay attention bestie: the behavior is undefined if there is more than one option in any of thecopy_optionsoption group present inoptions(even in the groups not relevant tocopy_file). Like, even the irrelevant groups. Do not test her. π
Then the actual copy logic, in order:
- If the destination file does not exist:
- it copies the contents and the attributes of the file to which
fromresolves to the file to whichtoresolves (symlinks are followed).
- it copies the contents and the attributes of the file to which
- Otherwise, if the destination file already exists:
- If
toandfromare the same as determined byequivalent(from, to), report an error. - Otherwise, if none of the
copy_filecontrol options are set inoptions, report an error. - Otherwise, if
copy_options::skip_existingis set inoptions, do nothing. (Iconic. Unbothered. Moisturized. π§) - Otherwise, if
copy_options::overwrite_existingis set inoptions, copy the contents and the attributes of the file to whichfromresolves to the file to whichtoresolves. - Otherwise, if
copy_options::update_existingis set inoptions, only copy the file iffromis newer thanto, as defined bylast_write_time().
- If
The non-throwing overloads return false if an error occurs. No throwing, just vibes π
π Parameters π
fromβ path to the source filetoβ path to the target fileecβ out-parameter for error reporting in the non-throwing overload
π Return value π
true if the file was copied, false otherwise. That's it, that's the whole mood π¦
π¦ Exceptions π¦
The overload that does not take an error_code& parameter throws filesystem_error on underlying OS API errors, constructed with from as the first argument, to as the second argument, and the OS error code as the error code argument. Also, std::bad_alloc may be thrown if memory allocation fails.
The overload taking an error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. This overload has this noexcept specification:
noexcept
β Notes β
- The functions involve at most one direct or indirect call to
status(to)(used both to determine if the file exists, and, forcopy_options::update_existingoption, its last write time). - Error is reported when
copy_fileis used to copy a directory: usecopyfor that. copy_filefollows symlinks: usecopy_symlinkorcopywithcopy_options::copy_symlinksfor that.
Lowkey the whole notes section is just "wrong tool for the job, bestie, here's the right one" and honestly? Respect π
π Example π
// Run this code
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
fs::create_directory("sandbox");
std::ofstream("sandbox/file1.txt").put('a');
fs::copy_file("sandbox/file1.txt", "sandbox/file2.txt");
// now there are two files in sandbox:
std::cout << "file1.txt holds : "
<< std::ifstream("sandbox/file1.txt").rdbuf() << '\n';
std::cout << "file2.txt holds : "
<< std::ifstream("sandbox/file2.txt").rdbuf() << '\n';
// fail to copy directory
fs::create_directory("sandbox/abc");
try
{
fs::copy_file("sandbox/abc", "sandbox/def");
}
catch (fs::filesystem_error& e)
{
std::cout << "Could not copy sandbox/abc: " << e.what() << '\n';
}
fs::remove_all("sandbox");
}
π Possible output π
file1.txt holds : a
file2.txt holds : a
Could not copy sandbox/abc: copy_file: Is a directory: "sandbox/abc", "sandbox/def"
Look at her go β copies the file, then gracefully catches the directory fail. Main character energy, no cap π₯
π See also π
copy_optionsβ specifies semantics of copy operations (enum)copy_symlinkβ copies a symbolic link (function)copyβ copies files or directories (function)
π Categories π
- Noindexed pages
- unconditionally noexcept
π§ Hidden categories π§
- Pages with unreviewed unconditional noexcept template
- Pages with unreviewed noexcept template
Okay that's the whole page, you're so smart now, periodt π She's from http://cppreference.com, last touched 2024.06.10, and the file is std::experi...::copy_file(3) in case you gotta cite your sources at the group chat π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms