How do I copy files in Visual C ++?
3 answers
What exactly do you want to do? If you need a copy of the data, you can read it and write it again. If you really want a copy of the file, you must use special OS calls.
In many cases, reading data in files and then reusing them in another file is a fairly close approximation of a copy - for example:
ifstream file1(...);
ofstream file2(...);
std::copy(istream_iterator<char>(file1),istream_iterator<char>(),ostream_iterator<char>(file2));
However, it really isn't a copy - it creates a new file with the same content. It will not handle hard links or symbolic links correctly, it will not handle metadata correctly, and it will "copy" the file by default .
If you need a copy of a file on Windows, you should call one of CopyFile , CopyFileEx, or CopyFileTransacted depending on your exact requirements.
+11
a source to share