Search code examples
c++windowsfilefstream

How can I copy and paste a file in Windows using C++?


I have googled this, but I am still confused about how to use it. I am making a file manager, and I want to be able t o copy and paste a file into a new directory. I know to copy I need to use file.copy(), but I am not sure how to implement it into my code.

I would like to do this using fstream.


Solution

  • If you are using the Win32 API then consider looking into the functions CopyFile or CopyFileEx.

    You can use the first in a way similar to the following:

    CopyFile( szFilePath.c_str(), szCopyPath.c_str(), FALSE );
    

    This will copy the file found at the contents of szFilePath to the contents of szCopyPath, and will return FALSE if the copy was unsuccessful. To find out more about why the function failed you can use the GetLastError() function and then look up the error codes in the Microsoft Documentation.