0

Please explain the syntax of: system(const char *command);

I want to use this function for running the command on unix sytem. I need to execute(automate) several test cases with the same command but,they also have other input values which are different.how do I reuse this code for all the test-cases.

9 Answers9

3
int main()
{
    char *base = "./your_testcase " ;
    char aux[50] = "./your_testcase " ;
    char *args[] = {"arg1" ,"arg2" ,"arg3"};
    int nargs = 3;

    for(i=0;i < nargs;i++)
    {
        /* Add arg to the end of the command */
        strcat(aux,args[i]) ;
        /* Call command with parameter */
        system(aux);
        /* Reset aux to just the system call with no parameters */
        strcpy(aux,base);
    }
}
Xetius
  • 44,755
  • 24
  • 88
  • 123
sud03r
  • 19,109
  • 16
  • 77
  • 96
3

Keep in mind that calling system is the same as calling fork and execl. That mean you need to be aware of things like open socket descriptors and file descriptors. I once had a problem with a TCP/IP socket dying on a server because a client was calling system which created a new socket connection to the server that was not being serviced.

2

I don't see how the syntax can be a problem:

system( "foo" );

executes the program called foo, via your preferred shell.

1

Generate a command line for each invokation, then pass those command lines into system() one a time.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
0

See also the question: 'How to call an external program with parameters?;

How to call an external program with parameters?

Community
  • 1
  • 1
Roger Nelson
  • 1,882
  • 2
  • 15
  • 21
0

I would avoid use of the system() function, here is a link to why this might be a bad idea

user134094
  • 41
  • 2
0

Here is the code, how to implement system() command in c++

#include <cstdlib>
 
 int main()
 {
    system("pause");
    return 0;
 }
Fei
  • 1,187
  • 1
  • 16
  • 35
  • Maybe you should read [this](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) before using `system("pause");` – Fei Jul 12 '13 at 08:14
0
system(const char *command)

Is used to execute a command on the command line of the current operating system. It is generally not the best idea to use this because the commands are platform specific. Keep in mind that const char *command is a string and you can pass any string value as a parameter and it will be sent to the command line.

Llewv
  • 123
  • 14
-1

i think Anter is interested in a example:

for instance to remove a file in a directory:

system("/bin/rm -rf /home/ederek/file.txt");

Vijay Angelo
  • 766
  • 6
  • 14