How do I copy a file from one folder to another folder using C++?
-
Do you want to use the command `cp` or write a clone for `cp`? – shadyabhi Feb 03 '12 at 07:29
-
i want to write a clone for cp – user1165435 Feb 03 '12 at 07:33
-
i don't want to execte commands. I after successful copy i want to delete the file from first folder. need help. appreciate – user1165435 Feb 03 '12 at 07:34
-
1"I after successful copy i want to delete the file from first folder."? You mean you want to **move** the file? – johnsyweb Feb 05 '12 at 07:53
-
1@Johnsyweb sounds like another X-Y problem. Instead of asking how to do what they actually want to do; they ask questions of how to various steps. At least this one is a useful step for people – UpAndAdam Sep 23 '16 at 19:22
5 Answers
With std::filesystem::copy_file
from C++17:
#include <exception>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
fs::path sourceFile = "path/to/sourceFile.ext";
fs::path targetParent = "path/to/target";
auto target = targetParent / sourceFile.filename(); // sourceFile.filename() returns "sourceFile.ext".
try // If you want to avoid exception handling, then use the error code overload of the following functions.
{
fs::create_directories(targetParent); // Recursively create target directory if not existing.
fs::copy_file(sourceFile, target, fs::copy_options::overwrite_existing);
}
catch (std::exception& e) // Not using fs::filesystem_error since std::bad_alloc can throw too.
{
std::cout << e.what();
}
}
I've used std::filesystem::path::filename
to retrieve the source filename without having to type it manually. However, with std::filesystem::copy
you can omit passing the filename to the target path at all:
fs::copy(sourceFile, targetParent, fs::copy_options::overwrite_existing);
Change the behaviour of both functions with std::filesystem::copy_options
.

- 7,933
- 6
- 68
- 80
This should be the minimal code required:
#include <fstream>
// copy in binary mode
bool copyFile(const char *SRC, const char* DEST)
{
std::ifstream src(SRC, std::ios::binary);
std::ofstream dest(DEST, std::ios::binary);
dest << src.rdbuf();
return src && dest;
}
int main(int argc, char *argv[])
{
return copyFile(argv[1], argv[2]) ? 0 : 1;
}
it glosses around some potentially complicated issues: error handling, filename character encodings... but could give you a start.

- 59,646
- 5
- 47
- 90
This is how you can do this.
include c++ library
<windows.h>
Use function
CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)
All Done :)

- 51,148
- 11
- 85
- 139

- 125
- 3
- 9
If you're willing to use the Boost C++ libraries, take a look at filesystem::copy_file().
Here's a previous question covering copy_file():
The code below will copy all the file from one directory to another.
Its working code in C++
#include <windows.h>
/*
BOOL Copy(char r_szPath[1024], char r_szDir[1024])
{
char l_szTemp[2048] = {0};
sprintf(l_szTemp,"%s\%s"r_szPath,r_szDir);
if(IsDirectory(
}*/
#include <stdio.h>
#include<conio.h>
BOOL __Copy(char r_szSrcPath[1024],char r_szDesPath[1024])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char l_szTmp[1025] = {0};
memcpy(l_szTmp,r_szSrcPath,1024);
char l_szSrcPath[1025] = {0};
char l_szDesPath[1025] = {0};
memcpy(l_szSrcPath,r_szSrcPath,1024);
memcpy(l_szDesPath,r_szDesPath,1024);
char l_szNewSrcPath[1025] = {0};
char l_szNewDesPath[1025] = {0};
strcat(l_szTmp,"*");
hFind = FindFirstFile(l_szTmp, &FindFileData);
if(hFind == NULL) return FALSE;
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(strcmp(FindFileData.cFileName,"."))
{
if(strcmp(FindFileData.cFileName,".."))
{
printf ("The Directory found is %s<BR>, FindFileData.cFileName);
sprintf(l_szNewDesPath,"%s%s\",l_szDesPath,FindFileData.cFileName);
sprintf(l_szNewSrcPath,"%s%s\",l_szSrcPath,FindFileData.cFileName);
CreateDirectory(l_szNewDesPath,NULL);
__Copy(l_szNewSrcPath,l_szNewDesPath);
}
}
}
else
{
printf ("The File found is %s<BR>, FindFileData.cFileName);
char l_szSrcFile[1025] = {0};
char l_szDesFile[1025] = {0};
sprintf(l_szDesFile,"%s%s",l_szDesPath,FindFileData.cFileName);
sprintf(l_szSrcFile,"%s%s",l_szSrcPath,FindFileData.cFileName);
BOOL l_bRet = CopyFile(l_szSrcFile,l_szDesFile,TRUE);
}
}
while(FindNextFile(hFind, &FindFileData));
FindClose(hFind);
return TRUE;
}
int main(int argc, char *argv[])
{
__Copy("C:\fcdb\","E:\sandy\");
getch();
return 0;
}

- 3,280
- 6
- 26
- 34

- 109
- 1
- 10
-
8welcome to SO... to improve your post consider deleting it or addressing these issues 1. this isn't working code, you have a swarm of errors in your if statements regarding '.' and '..'.. and don't forget the typo in your include where you omitted a space. 2. you didn't answer the question at hand. You answered a question you created. 3. the question is for C++ and Ubuntu... using windows specific stuff is not useful here 4. Please indent your code so that its readable – UpAndAdam Sep 23 '16 at 19:19