By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

what the problem with the "system" command

P: 57
Dear Friends,

For moving files I used the following code
sprintf(cpy,"mv %s%0.2f %s%0.2f","/var/data/lo,versionold,"/var/data/lo2",versionnew);

system(cpy);

Is there any problem using "system()". any portability issue? what are the defects using system()?

thanks,
john
Nov 11 '08 #1
Share this Question
Share on Google+
3 Replies


100+
P: 424
Your system() command will work if you can type that command in the console. In your example, the "mv" command is common to all posix systems but it won't work in windows. In general, system() commands should be avoided but one should also strike a balance between efficiency and portability; if your program will only ever need to run on a posix OS, then your system call will be fine.

The best solution is to make a comand movefile(char * old, char * new) whose responsibility it is to move the file. You can implement this command using a system() call or change your mind later and implement differently without affecting the rest of your code. You can even use #ifdef's to change the way movefile() is implemented depending on the OS it's compiled on.
Nov 11 '08 #2

P: 57
Thanks for the answer, but I want one more clarification.
which is better "system()" command or "exec" family of functions (eg:-execl,execv,execle,.. etc)
any how my program is going to run only in posix compatible systems.

Thanks,
john
Nov 13 '08 #3

Expert 100+
P: 671
which is better "system()" command or "exec" family of functions
They each do something different. Read very carefully what exec does.
Nov 13 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.