简体   繁体   中英

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...

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

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.

The code is also pretty much pure C. In C++, you can make it a bit more compact:

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.

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

#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. But you should have a look into Boost.FileSystem which enabled the functionality in a cross-platform fashion.

I think a good starting point is this example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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