Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

system( ) using cp

Posted on 2003-02-21
3
Medium Priority
?
232 Views
Last Modified: 2010-04-15
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");
0
Comment
Question by:pigyc
3 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 7993822
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);
0
 
LVL 11

Accepted Solution

by:
cup earned 200 total points
ID: 7994713
Minor point: why do you declare filenames of 256 characters and only allocate a command of 100 characters?

If you didn't want to do it using sprintf, you could use

#define PATH1 "/home/test/work/"
#define PATH2 " /home/test/ready/"  // leading space added
char commandstring[100];

strcpy (commandstring, "cp ");
strcat (commandstring, PATH1);
strcat (commandstring, "a.txt");
strcat (commandstring, PATH2);
stract (commandstring, "b.txt");
system(commandstring);

It may not be as elegant but the executable is a lot smaller.
0
 

Author Comment

by:pigyc
ID: 7994860
thanks all. Both are good answers.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question