Thread: How to invoke command line programatically?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    12

    How to invoke command line programatically?

    Hello,

    I want to issue a command line command from c program?

    As an example, I want to run a program like mplayer.

    How can i do this?

    Regards.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Use the system() command in stdlib.h, for example:


    Code:
    system("ls");

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Thank you.

    Is there a way to check if the statement execution is completed?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It appears like system() waits for the called process to finnish. This is from the man page.

    Quote Originally Posted by man page
    The system() function hands the argument command to the command interpreter sh(1). The
    calling process waits for the shell to finish executing the command, ignoring SIGINT and
    SIGQUIT, and blocking SIGCHLD.
    Other than that, I don't know. There might exist a more sophisticated method.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's up to the implementation (and the command interpreter or shell) but system() often waits for the command to finish and returns the exit code from the program invoked.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    On Windows, never use system() (crappy and prohibited by MS)
    Use Shell apis

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Alex31 View Post
    On Windows, never use system() (crappy and prohibited by MS)
    Use Shell apis
    Why do you think it's only crappy on Windows?

    "Prohibited by MS"?? Huh?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The return value of system() is the return code of the program which was executed. As long as the shell, and program, aren't brain-dead, this should be accurate. If system() returns 0, then the command executed successfully.

    Alex31, recommending a non-portable call when a portable one is perfectly sufficient is naive.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to move the command window programatically?
    By nacho4d in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2009, 12:38 PM
  2. Replies: 0
    Last Post: 09-26-2009, 12:33 PM
  3. invoke non-static function using ::
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2008, 01:29 AM
  4. Invoke IE
    By baddog in forum Windows Programming
    Replies: 2
    Last Post: 01-29-2008, 02:56 PM
  5. How to create and display a bitmap programatically?
    By solar3147 in forum Windows Programming
    Replies: 4
    Last Post: 06-24-2003, 02:55 AM