what the problem with the "system" command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mvjohn100
    New Member
    • Mar 2008
    • 57

    what the problem with the "system" command

    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
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    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.

    Comment

    • mvjohn100
      New Member
      • Mar 2008
      • 57

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

      Thanks,
      john

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        which is better "system()" command or "exec" family of functions
        They each do something different. Read very carefully what exec does.

        Comment

        Working...