Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. copying a file in C++17

copying a file in C++17

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
3 Posts 3 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    I wrote some code to copy a file:

    #include
    #include

    namespace fs = std::filesystem;

    int main() {
    fs::path source_file("source.txt");
    fs::path destination_file("destination.txt");

    try {
        fs::copy\_file(source\_file, destination\_file);
        std::cout << "File copied successfully!" << std::endl;
    }
    catch (const fs::filesystem\_error& e) {
        std::cerr << "Error copying file: " << e.what() << std::endl;
    }
    
    return 0;
    

    }

    Is this a good way of doing it? Anyone know what buffer sizes are being used behind the scenes? Thanks.

    L J 2 Replies Last reply
    0
    • M mike7411

      I wrote some code to copy a file:

      #include
      #include

      namespace fs = std::filesystem;

      int main() {
      fs::path source_file("source.txt");
      fs::path destination_file("destination.txt");

      try {
          fs::copy\_file(source\_file, destination\_file);
          std::cout << "File copied successfully!" << std::endl;
      }
      catch (const fs::filesystem\_error& e) {
          std::cerr << "Error copying file: " << e.what() << std::endl;
      }
      
      return 0;
      

      }

      Is this a good way of doing it? Anyone know what buffer sizes are being used behind the scenes? Thanks.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      mike7411 wrote:

      Anyone know what buffer sizes are being used behind the scenes?

      It is not recorded, but chances are it is calculated based on the size of the source file and the disk sector size. If you want to find out how particular STL features work then you can find lots of useful information at C++ reference - cppreference.com[^] or C++ Standard Library Reference | Microsoft Learn[^].

      1 Reply Last reply
      0
      • M mike7411

        I wrote some code to copy a file:

        #include
        #include

        namespace fs = std::filesystem;

        int main() {
        fs::path source_file("source.txt");
        fs::path destination_file("destination.txt");

        try {
            fs::copy\_file(source\_file, destination\_file);
            std::cout << "File copied successfully!" << std::endl;
        }
        catch (const fs::filesystem\_error& e) {
            std::cerr << "Error copying file: " << e.what() << std::endl;
        }
        
        return 0;
        

        }

        Is this a good way of doing it? Anyone know what buffer sizes are being used behind the scenes? Thanks.

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        mike7411 wrote:

        Is this a good way of doing it?

        1. You want to check what happens if different drives are involved. 2. You want to verify paths are supported. 3. Catching one type of exception ignores possible other ones. Probably unlikely but in case.

        mike7411 wrote:

        buffer sizes are being used behind the scenes?

        There are all sorts of possible buffers. Disk, OS, library. Only concern however for that is speed. You can profile it. You can also use a OS command shell call for comparison. If it matters, at least in my experience, OS shell commands will always be faster. This is especially true when copying directories. Seems reasonable given that the copy operation in the OS doesn't involve loading the data into the application. Even so if you need it to be 'fast' for some reason then I would suggest that you need to change your requirements/design. Copying files, in general, is always 'slow'. Speed doesn't matter for single small files. So only matters for very large files and/or large numbers of files. But those will always be 'slow'. And there can be error conditions that make it even slower (which your code does not account for.) So attempting to guarantee a speed rate is never going to work.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups