如何从一个路径在C++中的文件夹复制到另一个
HI ......任何一个可以给我提供了如何将文件从一个目录复制到另一个......我能够通过这个代码将文件复制的代码,但我不能照搬entir文件夹在同一时间...如何从一个路径在C++中的文件夹复制到另一个
FILE *in, *out;
char ch;
if((in=fopen(sour->getfilepath(), "rb")) == NULL)
{
printf("Cannot open input file.\n");
getch();
exit(1);
}
if((out=fopen(dest->getfilepath(), "wb")) == NULL)
{
printf("Cannot open output file.\n");
getch();
exit(1);
}
while(!feof(in))
{
ch = getc(in);
if(ferror(in))
{
printf("Read Error");
clearerr(in);
break;
}
else
{
if(!feof(in)) putc(ch, out);
if(ferror(out))
{
printf("Write Error");
clearerr(out);
break;
}
}
}
fclose(in);
fclose(out);
湿婆,我不相信有一个标准库函数来做到这一点。我认为你必须递归迭代并创建目录并随时复制文件。我敢打赌,你可以找到源代码,这是否code.google.com上或www.krugle.com
我不是真正熟悉C++,但也许你可以在你的目的地创建要被复制的文件夹目录,并复制到该文件夹中的所有文件...我想你可以实现你想要的方式...我只是觉得没有内置的例程在C++中可以做到这一点,我的意思是像在Java中,如果我又不是错了,你只能复制单个文件...我希望我没有错..只是一个想法...
怎么样的系统调用像系统(命令)。
这不是平台独立的,如果你打算做这样的事情,我只是使用winapi函数(或等价的* nix函数)这将更快地看到他们如何做同样的事情,但没有开始一个单独的过程去做。 – 2010-02-10 05:45:22
如果你想和可移植的代码要做到这一点,你应该考虑使用升压文件系统库。对于稍微小一点的可移植性,您可以使用Posix目录函数(opendir,readdir,closedir,chdir等)。如果您根本不关心可移植性,则可能需要使用特定于系统的内容(例如,在Windows FindFirstFile ,FindNextFile,FindClose,SetCurrentDirectory)。
至于实际文件拷贝去,你的代码可能不会在现实生活中很好地工作 - 比如,你平时不与开放,你试图打开一个文件正确报告问题。根据所涉及的程序种类,您可以将其写入日志或在状态窗口中显示。即使您将命令行用法视为理所当然,它仍然几乎肯定会写入stderr而不是标准输出。
代码也非常纯C.在C++中,你可以把它多一点紧凑:
std::ifstream in(sour->GetFilePath());
std::ofstream out(dest->GetFilePath());
out << in.rdbuf();
眼下,这种忽略错误 - 有一些处理这些方法(例如在iostream中启用异常),因此很难在不知道您真正想要的情况下为其显示代码。
C++标准库不支持文件夹操作。但你应该看看Boost.FileSystem,它以跨平台的方式启用了功能。
我认为一个好的出发点是this example。
我看到它与[C++ 17的文件系统静态类](http://en.cppreference.com/w/cpp/filesystem)。我的OSX在2016年还没有它(最新的OSX更新)。我尝试了'#include
#include <iostream>
#include <string>
#include <fstream>
#include <dirent.h>
void copyFile(const char* fileNameFrom, const char* fileNameTo){
char buff[BUFSIZ];
FILE *in, *out;
size_t n;
in = fopen(fileNameFrom, "rb");
out = fopen(fileNameTo, "wb");
while ((n=fread(buff,1,BUFSIZ,in)) != 0) {
fwrite(buff, 1, n, out);
}
}
int dir(std::string path){
DIR *dir;
struct dirent *ent;
if ((dir = opendir (path.c_str())) != NULL) {
while ((ent = readdir (dir)) != NULL) { /* print all the files and directories within directory */
if (ent->d_name != std::string(".")){ //REMOVE PWD
if (ent->d_name != std::string("..")){ //REMOVE PARENT DIR
std::cout << path << "\\" << ent->d_name << std::endl;
}
}
}
std::cout << std::endl;
closedir (dir);
}else{
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
return 0;
}
int copyAllFiles(std::string sorc, std::string dest){
DIR *dir;
struct dirent *ent;
if ((dir = opendir (sorc.c_str())) != NULL) {
while ((ent = readdir (dir)) != NULL) { /* print all the files and directories within directory */
if (ent->d_name != std::string(".")){ //REMOVE PWD
if (ent->d_name != std::string("..")){ //REMOVE PARENT DIR
std::string copy_sorc = sorc + "\\" + ent->d_name;
std::string copy_dest = dest + "\\" + ent->d_name;
std::cout << "cp " << copy_sorc << " -> " << copy_dest << std::endl;
copyFile(copy_sorc.c_str(), copy_dest.c_str());
}
}
}
std::cout << std::endl;
closedir (dir);
}else{
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
return 0;
}
int main(int argc, char* argv[]){
//dir("C:\\example"); //SHOWS CONTENT IN FOLDER
copyAllFiles("C:\\example", "C:\\destination"); //COPY FOLDER'S CONTENT
return 0;
}
你赌输了史密斯先生我不力找到代码 – 2010-02-10 11:00:39
湿婆,去http://www.google.com/codesearch输入以下搜索项** INT copyDirectory \(常量郎咸平:C ** ,并在结果中寻找** libs/host/CopyFile.c ** – 2010-02-10 18:53:42