Sign in to follow this  
thannett

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this