?
Solved

system( ) using cp

Posted on 2003-02-21
3
Medium Priority
?
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

765 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