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
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
#include
//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 buffer;
buffer = std::get_temporary_buffer(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
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.