How to copy a file from a folder to another folder

时间:2022-03-14 06:25:41

How do I copy a file from one folder to another folder using C++?

如何使用C ++将文件从一个文件夹复制到另一个文件夹?

4 个解决方案

#1


8  

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.

它掩盖了一些可能复杂的问题:错误处理,文件名字符编码......但可以给你一个开始。

#2


3  

With std::filesystem::copy_file from C++17:

使用来自C ++ 17的std :: filesystem :: copy_file:

#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:

我已经使用std :: filesystem :: path :: filename来检索源文件名而无需手动输入。但是,使用std :: filesystem :: copy,您可以省略将文件名传递到目标路径:

fs::copy(sourceFile, targetParent, fs::copy_options::overwrite_existing);

Change the behaviour of both functions with std::filesystem::copy_options.

使用std :: filesystem :: copy_options更改这两个函数的行为。

#3


1  

If you're willing to use the Boost C++ libraries, take a look at filesystem::copy_file().

如果您愿意使用Boost C ++库,请查看filesystem :: copy_file()。

Here's a previous question covering copy_file():

这是一个覆盖copy_file()的上一个问题:

How to use copy_file in boost::filesystem?

如何在boost :: filesystem中使用copy_file?

#4


-1  

The code below will copy all the file from one directory to another.

下面的代码将所有文件从一个目录复制到另一个目录。

Its working code in C++

它的工作代码用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;
}

#1


8  

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.

它掩盖了一些可能复杂的问题:错误处理,文件名字符编码......但可以给你一个开始。

#2


3  

With std::filesystem::copy_file from C++17:

使用来自C ++ 17的std :: filesystem :: copy_file:

#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:

我已经使用std :: filesystem :: path :: filename来检索源文件名而无需手动输入。但是,使用std :: filesystem :: copy,您可以省略将文件名传递到目标路径:

fs::copy(sourceFile, targetParent, fs::copy_options::overwrite_existing);

Change the behaviour of both functions with std::filesystem::copy_options.

使用std :: filesystem :: copy_options更改这两个函数的行为。

#3


1  

If you're willing to use the Boost C++ libraries, take a look at filesystem::copy_file().

如果您愿意使用Boost C ++库,请查看filesystem :: copy_file()。

Here's a previous question covering copy_file():

这是一个覆盖copy_file()的上一个问题:

How to use copy_file in boost::filesystem?

如何在boost :: filesystem中使用copy_file?

#4


-1  

The code below will copy all the file from one directory to another.

下面的代码将所有文件从一个目录复制到另一个目录。

Its working code in C++

它的工作代码用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;
}