![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c system call | rangaswamy | High Level Programming | 1 | 02-19-2008 09:53 AM |
| how to differentiate system call from library call | muru | UNIX for Advanced & Expert Users | 2 | 07-19-2007 08:20 PM |
| Problem in system call | spmlingam | High Level Programming | 1 | 04-03-2006 03:02 AM |
| a system call for sed in a awk script | seaten | UNIX for Dummies Questions & Answers | 6 | 05-20-2005 07:17 AM |
| wait system call | a25khan | High Level Programming | 2 | 02-11-2004 07:56 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
can we send arguments to system() call
Hi friends
my C code is int main() { system("cp <source> <destination>"); } my question is how to set variables for <source> and <destination> how can we pass it to system() call. can you suggest me thankyou kingskar |
| Forum Sponsor | ||
|
|
|
|||
|
In such situations it's always better to do a combination of fork and one of the exec family member.
if(fork()==0){ //do nothing } else{ exec....( what ever suits you) } You can also check the exec man page to see the all members of exec family!! regards Apoorva kumar |
|
|||
|
vfork and exec - that is what is done in system function call..
and to answer your question, something like this would do.. Code:
/* source file in char source[512] and dest file in char dest[512] */ char str[512]; memset(str, '\0', sizeof(512)); sprintf(str, "/usr/bin/cp %s %s", source, dest); ret=system(str); |
|
|||
|
Quote:
Yes your code is exactly fits for my requirement. Thankyou Cheers kingskar |
|
|||
|
if you are using glibc it may be better to use:
#define _GNU_SOURCE #include <stdio.h> char *buf; asprintf(&buf,"cp %s/%s",src,dst); /* do not forget error check */ system(buf); free(buf); the advantage is that you have no limits for pathlenght. you can also emulate asprintf with 2 calls to snprintf() |