Jump to content
  • Advertisement
Sign in to follow this  
thannett

Copy Command

This topic is 4949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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!!!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!