如何将文件从一个文件夹复制到另一个文件夹 我如何用C++将文件从一个文件夹复制到另一个文件夹?< P> >如果你愿意使用Boost C++库,请看./P>
这里有一个关于复制文件()的问题:如何将文件从一个文件夹复制到另一个文件夹 我如何用C++将文件从一个文件夹复制到另一个文件夹?< P> >如果你愿意使用Boost C++库,请看./P>,c++,linux,file-io,C++,Linux,File Io,这里有一个关于复制文件()的问题: 这应该是所需的最低代码: #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.rdb
这应该是所需的最低代码:
#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;
}
#包括
//以二进制模式复制
bool copyFile(常量字符*SRC,常量字符*DEST)
{
std::ifstream src(src,std::ios::binary);
std::ofstreamdest(dest,std::ios::binary);
dest下面的代码将所有文件从一个目录复制到另一个目录
它的工作代码是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;
}
#包括
/*
BOOL复制(char r_szPath[1024],char r_szDir[1024])
{
charl_szTemp[2048]={0};
sprintf(l_szTemp,“%s\%s”r_szPath,r_szDir);
if(IsDirectory)(
}*/
#包括
#包括
BOOL\uuuu Copy(charr\u szSrcPath[1024],charr\u szDesPath[1024])
{
WIN32_FIND_DATA FindFileData;
处理高频风;
charl_szTmp[1025]={0};
memcpy(l_szTmp,r_szSrcPath,1024);
charl_szSrcPath[1025]={0};
charl_szDesPath[1025]={0};
memcpy(l_szSrcPath,r_szSrcPath,1024);
memcpy(l_szDesPath,r_szDesPath,1024);
charl_szNewSrcPath[1025]={0};
charl_szNewDesPath[1025]={0};
strcat(l_szTmp,“*”);
hFind=FindFirstFile(l_szTmp和FindFileData);
if(hFind==NULL)返回FALSE;
做
{
if(FindFileData.dwFileAttributes和文件属性目录)
{
if(strcmp(FindFileData.cFileName,“.”)
{
if(strcmp(FindFileData.cFileName,“…”)
{
printf(“找到的目录是%s
,FindFileData.cFileName);
sprintf(l_szNewDesPath,“%s%s\”,l_szDesPath,FindFileData.cFileName);
sprintf(l_szNewSrcPath,“%s%s\”,l_szSrcPath,FindFileData.cFileName);
CreateDirectory(l_szNewDesPath,NULL);
__副本(l_szNewSrcPath,l_szNewDesPath);
}
}
}
其他的
{
printf(“找到的文件是%s
,FindFileData.cFileName);
charl_szSrcFile[1025]={0};
charl_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);
返回TRUE;
}
int main(int argc,char*argv[])
{
__副本(“C:\fcdb\”,“E:\sandy\”;
getch();
返回0;
}
使用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();
}
}
使用更改这两个函数的行为。这就是您可以执行此操作的方法
1.包括C++库<代码> <代码> <
2.使用功能
CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)
3.全部完成:)是否要使用命令cp
或为cp
编写克隆?我想为cpi编写克隆不想执行命令。复制成功后,我想从第一个文件夹中删除文件。需要帮助。感谢“复制成功后,我想从第一个文件夹中删除文件。”你的意思是你想移动文件吗?@ JangSyWeb听起来像是另一个X-Y问题,而不是问如何做他们真正想做的事,他们问如何步骤的问题。至少这是一个有用的步骤,人们欢迎这样…改进你的帖子考虑删除它或解决这些问题1。这不是工作代码,哟。你在你的if语句中有一大堆错误关于……和…..而且不要忘记你的省略中的排错。2。你没有回答手边的问题。你回答了一个你创建的问题。3。问题是C++和ubuntu……使用Windows特定的东西在这里不有用。请缩进你的代码以便它的C++文件是C++ 14启动后C++的一部分。
CopyFile("d:/folder1/file.exe","d:/folder2/file.exe",true)