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

25,129

Solution 1

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.

Solution 2

void copyFile(const std::string &from, const std::string &to)
{
    std::ifstream is(from, ios::in | ios::binary);
    std::ofstream os(to, ios::out | ios::binary);

    std::copy(std::istream_iterator(is), std::istream_iterator(),
          std::ostream_iterator(os));
}

Solution 3

Here is my implementation to copy a file, you should take a look at boost filesystem since that library will be part of the standard c++ library.

#include <fstream>
#include <memory>

//C++98 implementation, this function returns true if the copy was successful, false otherwise.

bool copy_file(const char* From, const char* To, std::size_t MaxBufferSize = 1048576)
{
    std::ifstream is(From, std::ios_base::binary);
    std::ofstream os(To, std::ios_base::binary);

    std::pair<char*,std::ptrdiff_t> buffer;
    buffer = std::get_temporary_buffer<char>(MaxBufferSize);

    //Note that exception() == 0 in both file streams,
    //so you will not have a memory leak in case of fail.
    while(is.good() and os)
    {
       is.read(buffer.first, buffer.second);
       os.write(buffer.first, is.gcount());
    }

    std::return_temporary_buffer(buffer.first);

    if(os.fail()) return false;
    if(is.eof()) return true;
    return false;
}

#include <iostream>

int main()
{
   bool CopyResult = copy_file("test.in","test.out");

   std::boolalpha(std::cout);
   std::cout << "Could it copy the file? " << CopyResult << '\n';
}

The answer of Nisarg looks nice, but that solution is slow.

Solution 4

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx

I don't know what you mean by copy and paste a file; that makes no sense. You can copy a file to another location and I assume that's what you are asking about.

Share:
25,129
Admin
Author by

Admin

Updated on August 02, 2022

Comments

  • Admin
    Admin over 1 year

    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.

  • Admin
    Admin over 10 years
    I am not using WinAPI
  • Admin
    Admin over 10 years
    That is what I meant.
  • Jon Purdy
    Jon Purdy over 10 years
    Should be istream_iterator<char>, or better yet istreambuf_iterator<char>. Likewise for ostream.
  • Remy Lebeau
    Remy Lebeau over 10 years
    No, but you did say you are working "in Windows", which means you have access to the Win32 API. There is no standard C++ wrapper that does the same thing CopyFile/Ex() does. If you want a purely C++ solution, you would have to create and open the destination file, and then manually loop through the source file copying bytes to the destination file, like Nisarg and dieram3 showed. Not as efficient as using a native OS solution.
  • Cody Gray
    Cody Gray over 10 years
    I strongly recommend calling the API function instead of rolling your own.
  • T.Coutlakis
    T.Coutlakis about 10 years
    This doesn't work in Windows 2008 R2, but it works in 2003. Why?
  • László Papp
    László Papp about 10 years
    Ali, could you please remove this answer? There are a couple of existing and better answers, already. If you have a critic to any of them, just gain enough reputation for commenting. At the very least, provide some explanation, including some MSDN documentation, et cetera. It is a low-quality post as it stands.
  • Chaohsiung  Huang
    Chaohsiung Huang about 2 years
    what does this line means? while(is.good() and os)