Copy a file in a sane, safe and efficient way

前端 未结 7 1453
小蘑菇
小蘑菇 2020-11-22 07:11

I search for a good way to copy a file (binary or text). I\'ve written several samples, everyone works. But I want hear the opinion of seasoned programmers.

I missin

7条回答
  •  时光取名叫无心
    2020-11-22 07:18

    For those who like boost:

    boost::filesystem::path mySourcePath("foo.bar");
    boost::filesystem::path myTargetPath("bar.foo");
    
    // Variant 1: Overwrite existing
    boost::filesystem::copy_file(mySourcePath, myTargetPath, boost::filesystem::copy_option::overwrite_if_exists);
    
    // Variant 2: Fail if exists
    boost::filesystem::copy_file(mySourcePath, myTargetPath, boost::filesystem::copy_option::fail_if_exists);
    

    Note that boost::filesystem::path is also available as wpath for Unicode. And that you could also use

    using namespace boost::filesystem
    

    if you do not like those long type names

提交回复
热议问题