tags:

views:

231

answers:

5

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);
+3  A: 

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

Scott Smith
you lost the bet Mr.Smith i dint find code
SHIVA KUMAR
Shiva, go to http://www.google.com/codesearch enter the following search term **int copyDirectory\(const lang:c**, and look for **libs/host/CopyFile.c** in the results.
Scott Smith
A: 

i am not really familiar with c++ but maybe you can create the to-be-copied folder in your destination dir, and copy to that folder all the files... i guess you can achieve what you want that way... i just feel there is no built-in routine in c++ that can do that, i mean like in java if i'm not mistaken, you can only copy individual files... i hope i'm not wrong.. just a thought...

ultrajohn
A: 

what about system calls like system(command).

Ashish
thats not platform independent, if you were going to do something like that, I'd just use the winapi functions (or equivalent *nix functions) which would be faster seeing how they do the same thing but without starting a separate process to do it.
Grant Peters
+4  A: 

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 (e.g. 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 (e.g. enabling exceptions in the iostreams) so it's hard to show code for that without knowing what you really want.

Jerry Coffin
A: 

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.

beef2k