简体   繁体   English

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

[英]how to copy folder from one path to another in c++

HI...Can any one provide me the code for how to copy the folder from one directory to another...i am able to copy the files by this code but i cannot copy the entir folder at a time... HI ...任何人都可以提供如何将文件夹从一个目录复制到另一个目录的代码...我可以通过此代码复制文件,但我不能一次复制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);

If you want to do this with portable code, you should probably look into using the Boost filesystem library. 如果您想使用可移植代码执行此操作,则应该考虑使用Boost文件系统库。 For slightly less portability, you can probably use the Posix directory functions (opendir, readdir, closedir, chdir, etc.) If you don't care about portability at all, you may have something system-specific to use (eg on Windows FindFirstFile, FindNextFile, FindClose, SetCurrentDirectory). 对于稍微不那么便携,你可以使用Posix目录函数(opendir,readdir,closedir,chdir等)如果你根本不关心可移植性,你可能会有一些特定于系统的东西(例如在Windows FindFirstFile上) ,FindNextFile,FindClose,SetCurrentDirectory)。

As far as the actual file copying goes, your code probably won't work very well in real life -- for example, you usually don't report a problem with opening a file right where you tried to open it. 就实际文件复制而言,您的代码在现实生活中可能无法正常工作 - 例如,您通常不会在尝试打开文件时报告打开文件的问题。 Depending on the kind of program involved, you might write that to a log or show it in a status window. 根据所涉及的程序类型,您可以将其写入日志或在状态窗口中显示。 Even if you take command-line use for granted, it should still almost certainly be written to stderr, not stdout. 即使您将命令行使用视为理所当然,它仍然应该写入stderr,而不是stdout。

The code is also pretty much pure C. In C++, you can make it a bit more compact: 代码也非常纯粹C.在C ++中,你可以使它更紧凑:

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

out << in.rdbuf();

Right now, this ignores errors -- there are a number of ways of handling those (eg enabling exceptions in the iostreams) so it's hard to show code for that without knowing what you really want. 现在,这忽略了错误 - 有许多方法可以处理这些错误(例如在iostream中启用异常),因此很难在不知道你真正需要的情况下显示代码。

Shiva, I don't believe that there is a standard library function to do this. 湿婆,我不相信有一个标准的库函数来做到这一点。 I think you have to recursively iterate and create directories and copy files as you go. 我认为你必须递归迭代并创建目录并复制文件。 I'd bet you can find source code that does this on code.google.com or www.krugle.com 我敢打赌,您可以在code.google.com或www.krugle.com上找到执行此操作的源代码

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

我对c ++并不熟悉,但也许你可以在目的地目录中创建待复制的文件夹,然后将所有文件复制到该文件夹​​......我想你可以通过这种方式实现你想要的...我只是觉得c ++中没有内置的例程可以做到这一点,我的意思是在java中,如果我没有弄错,你只能复制单个文件...我希望我没错。只是一个想法.. 。

系统调用如system(command)怎么样?

The C++ standard libraries do not support folder operations. C ++标准库不支持文件夹操作。 But you should have a look into Boost.FileSystem which enabled the functionality in a cross-platform fashion. 但是你应该看看Boost.FileSystem ,它以跨平台的方式启用了这个功能。

I think a good starting point is this example . 我认为这个例子是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM