如何将文件从一个文件夹复制到另一个文件夹

9

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


你想使用命令 cp 还是编写 cp 的克隆版本? - shadyabhi
我想要写一个cp的克隆版本。 - user1165435
我不想执行命令。在成功复制后,我想从第一个文件夹中删除该文件。需要帮助。感激不尽。 - user1165435
1
我在成功复制后想要从第一个文件夹中删除该文件。你的意思是你想要移动这个文件吗? - johnsyweb
1
@Johnsyweb 听起来像另一个 X-Y 问题。与其问如何做他们实际想要做的事情,他们会问如何执行各个步骤的问题。至少这个问题对人们来说是有用的一步。 - UpAndAdam
5个回答

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();
    }
}

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

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

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


16

以下是所需的最小代码:

#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;
}

它忽略了一些潜在的复杂问题:错误处理、文件名字符编码... 但可以帮助你入门。


2

2
现在,copy_file 是 C++14 开始的一部分。 - TheArchitect

2
这是如何完成此操作的方法。
  1. include c++ library <windows.h>

  2. Use function

    CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)
    
  3. All Done :)


-1
下面的代码将把一个目录中的所有文件复制到另一个目录中。
这是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;
}

8
欢迎来到SO...为了改进你的帖子,请考虑删除它或解决以下问题:1. 这不是有效的代码,你在if语句中使用'.'和'..'时出现了大量错误。还有别忘了你的include中有一个省略了空格的错别字。2. 你没有回答手头的问题。你回答了一个你自己创建的问题。3. 这个问题是关于C++和Ubuntu的...使用Windows特定的东西在这里没有用处。4. 请缩进你的代码以便阅读。 - UpAndAdam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接