Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c+中将文件夹从一个路径复制到另一个路径+;_C++ - Fatal编程技术网

C++ 如何在c+中将文件夹从一个路径复制到另一个路径+;

C++ 如何在c+中将文件夹从一个路径复制到另一个路径+;,c++,C++,嗨…有谁能给我提供如何将文件夹从一个目录复制到另一个目录的代码吗…我可以用这个代码复制文件,但我不能一次复制entir文件夹 FILE *in, *out; char ch; if((in=fopen(sour->getfilepath(), "rb")) == NULL) { printf("Cannot open input file.\n"); getch(); exit(1); } i

嗨…有谁能给我提供如何将文件夹从一个目录复制到另一个目录的代码吗…我可以用这个代码复制文件,但我不能一次复制entir文件夹

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

Shiva,我不相信有标准的库函数可以做到这一点。我认为您必须递归地迭代和创建目录,并在运行时复制文件。我敢打赌,你可以找到代码,这在代码。GoGoLe.com或www. krurg.com。/p> < p>我不太熟悉C++,但也许你可以在你的目的地DIR中创建要复制的文件夹,并将所有文件复制到该文件夹…我想你可以通过这种方式实现你想要的。。。我觉得C++中没有内置的例程,我想说,如果在java中,我没有错,你只能拷贝单个文件…我希望我没有错。。只是想一想…

像system(command)这样的系统调用怎么样。

如果您想用可移植代码实现这一点,您可能应该考虑使用Boost文件系统库。对于稍低的可移植性,您可能可以使用Posix目录函数(opendir、readdir、closedir、chdir等)。如果您根本不关心可移植性,您可以使用特定于系统的功能(例如,在Windows FindFirstFile、FindNextFile、FindClose、SetCurrentDirectory上)

就实际的文件复制而言,您的代码在现实生活中可能不会很好地工作——例如,您通常不会在试图打开文件的地方报告打开文件的问题。根据所涉及的程序类型,您可以将其写入日志或在状态窗口中显示。即使您认为命令行的使用是理所当然的,也几乎可以肯定它应该被写入到stderr,而不是stdout

<>代码在C++中也相当纯C.,可以使它更紧凑:

std::ifstream in(sour->GetFilePath());
std::ofstream out(dest->GetFilePath());

out << in.rdbuf();
std::ifstream-in(sour->GetFilePath());
std::ofstreamout(dest->GetFilePath());

Ex>

C++标准库不支持文件夹操作。但是,您应该看看是什么以跨平台的方式启用了该功能

我认为一个好的起点是包括 #包括 #包括 #包括 无效复制文件(const char*fileNameFrom,const char*fileNameTo){ 字符buff[BUFSIZ]; 文件*输入,*输出; 尺寸; in=fopen(fileNameFrom,“rb”); out=fopen(fileNameTo,“wb”); 而((n=fread(buff,1,BUFSIZ,in))!=0){ fwrite(buff,1,n,out); } } int-dir(标准::字符串路径){ DIR*DIR; 结构导向; if((dir=opendir(path.c_str())!=NULL){ 而((ent=readdir(dir))!=NULL){/*打印目录中的所有文件和目录*/ 如果(ent->d_name!=std::string(“.”){//删除PWD 如果(ent->d_name!=std::string(“…”){//删除父目录 std::cout d_name;
std::cout这不是平台独立的,如果你要做类似的事情,我只会使用winapi函数(或等效的*nix函数),这会更快地看到它们如何做同样的事情,但不需要启动单独的进程。Shiva,进入以下搜索词int copyDirectory(const lang:c,并在结果中查找libs/host/CopyFile.c。我看到了它。我的OSX在2016年还没有它(OSX的最新更新)。我尝试了
#include
,XCode说找不到该文件。
#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;
}