如何使用C++將文件從一個文件夾複製到另一個文件夾?如何將文件從一個文件夾複製到另一個文件夾
回答
如果您願意使用Boost C++庫,請參閱filesystem::copy_file()。
這裏是前一個問題涉及COPY_FILE():
現在copy_file是自C++ 14以來的C++ start的一部分。 – Tecoberg 2017-08-28 10:51:52
這應該是需要的最小代碼:
#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;
}
它掩蓋周圍的一些潛在的複雜問題:錯誤處理,文件名字符編碼......但可以給你一個開始。
下面的代碼會將所有文件從一個目錄複製到另一個目錄。
其從C++ 17在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;
}
歡迎來到SO ...以改善你的帖子考慮刪除它或解決這些問題1.這不是工作代碼,你在if語句中有一堆關於'。'的錯誤。和'..'..並且不要忘記包含在你省略空格的錯字。你沒有回答這個問題。你回答了你創建的問題。 3.問題是針對C++和Ubuntu ...使用特定於Windows的東西在這裏沒有用處4.請縮進您的代碼以使其可讀 – UpAndAdam 2016-09-23 19:19:02
隨着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
。
- 1. 將png文件從一個文件夾複製到另一個
- 2. 將x文件從一個文件夾複製到另一個
- 3. 將文件夾從一個文件夾複製到一個新文件夾
- 4. 將文件夾從一組文件複製到另一個文件夾
- 5. Applescript - 將文件從當前文件夾複製到另一個文件夾
- 6. 使用vbscript將文件從一個文件夾複製到另一個文件
- 7. 如何在VB中將文本文件從一個文件夾複製到另一個文件夾?
- 8. 如何從命令提示符將特定文件從一個文件夾複製到另一個文件夾
- 9. 如何將文件從一個文件夾複製到另一個
- 10. 如何將一百個文件從文件夾複製到文件夾?
- 11. 如何使用Java API將文件從一個HDFS文件夾複製到另一個HDFS文件夾?
- 12. 如何使用js將文件從一個文件夾複製到另一個文件夾
- 13. 如何使用bash腳本將所有* .html文件從一個文件夾複製到另一個文件夾?
- 14. 基於類似的文件名將文件從一個文件夾複製到另一個文件夾
- 15. 將文件從一個文件夾複製到同一個桶的s3中的另一個文件夾NODE JS
- 16. 將文件從一個文件夾複製到其他文件
- 17. PowerShell的最新文件夾複製到另一個文件夾
- 18. 複製文件夾到另一個文件夾
- 19. 將zip文件夾中的文件複製到另一個文件夾
- 20. 如何將文件夾和子文件夾中的文件複製到R中的另一個文件夾?
- 21. 需要將文件從一個文件夾移動到另一個文件夾
- 22. VSS:將文件從一個文件夾移動到另一個文件夾?
- 23. UNIX shell將文件從一個文件夾移動到另一個文件夾
- 24. 隨機將文件從一個文件夾移動到另一個文件夾?
- 25. 複製文件夾到另一個
- 26. 將圖像從一個文件夾複製到另一個
- 27. 需要使用jmeter將文本文件從一個文件夾複製到另一個文件夾中
- 28. 將文件複製到另一個文件夾
- 29. Windows將文件從子文件夾批量複製到一個文件夾
- 30. NodeJS - 如何將一個文件夾複製到另一個文件夾,只覆蓋不同的文件?
你想使用命令'cp'還是爲'cp'編寫一個克隆? – 2012-02-03 07:29:25
我想寫一個克隆cp – user1165435 2012-02-03 07:33:26
我不想執行命令。我成功複製後,我想從第一個文件夾中刪除該文件。需要幫忙。欣賞 – user1165435 2012-02-03 07:34:06