Reputation: 81
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);
Upvotes: 2
Views: 10234
Reputation: 131
#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;
}
Upvotes: 0
Reputation: 2257
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.
Upvotes: 0
Reputation: 490713
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.
Upvotes: 4
Reputation: 2599
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...
Upvotes: 0
Reputation: 3986
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
Upvotes: 3