Link to home
Start Free TrialLog in
Avatar of pigyc
pigyc

asked on

system( ) using cp

hi,
need help immediately for a c system call. I tried to copy one file from one directory to anther directory but did not workout. if do i this it will work:
system("cp /home/test/work/a.txt  /home/test/ready/b.txt")

but if i do this it did not work, any hints ?:
#define PATH1 "/home/test/work/%s"
#define PATH2 "/home/test/ready/%s"
char file11[256], file2[256];

sprintf(file1, PATH1, "a.txt");
sprintf(file2, PATH2, "b.txt");
system("cp file1 file2");
Avatar of gj62
gj62

That's because you can't send variable parameters to the system call in quotes.  What actually went to system was:

cp file1 file2, not your actual command string.

instead do this

#define PATH1 "/home/test/work/%s"
#define PATH2 "/home/test/ready/%s"
char file1[256], file2[256];
char commandstring[100];

sprintf(file1, PATH1, "a.txt");
sprintf(file2, PATH2, "b.txt");
sprintf(commandstring, "cp %s %s", file1, file2);
system(commandstring);
ASKER CERTIFIED SOLUTION
Avatar of cup
cup

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pigyc

ASKER

thanks all. Both are good answers.