# Copy Command

## Recommended Posts

Hi, I was wondering if there was a copy command in C++? I know that you can use system() calls, but I want to copy to a directory that I have created, but whose name will change daily. I have the name of the directory stored in a string variable. Sorry if I was not clear. Thanks!

##### Share on other sites
Well, if you know what the name is, all you have to do is compute the command at runtime, and pass it to system.

Such as:

#include <string>using std::string;//...string sourcedir = "/usr/local/";string objname = "stuff.txt";string destination = "/usr/backup/";//...string command = string("cp ") + sourcedir + objname + " " + destination + objname;system( command.c_str( ) );

##### Share on other sites
The most efficient means are platform specific. That said, however, you can achieve the same effect as follows:
#include <algorithm>#include <fstream>#include <iostream>#include <iterator>int main(int argc, char * argv[]){  // arg 1: path to source file  // arg 2: path to dest file  //*** assume data validation  using namespace std;  ifstream fin(argv[1]);  ofstream fout(argv[2]);  istream_iterator<char> isi(fin), eos;  ostream_iterator<char> osi(fout);  copy(isi, eos, osi);  fout.close();  fin.close();  return 0;}

##### Share on other sites
Thanks guys!!!!

I ended up using ToohrVyk's solution, mainly because I'm not the greatest programmer and it took less time for me to understand.

Anyways, Thank you both very much!!!!

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628346
• Total Posts
2982203

• 10
• 9
• 24
• 11
• 9