how to call system command with variable parameters?

Discussion in 'C++' started by shaggystyle, Feb 18, 2004.

  1. shaggystyle

    shaggystyle Guest

    I'm looking for a way to call system commands with variable parameters
    within a Unix environment. Sort of like system() but i need to be
    able to pass variable parameters....so I guess it would kind of look
    like this:

    system("command -%s -%s", parameter1, parameter2);

    I've tried the above and it doesn't work when compiled with GCC.
    Thanks.
     
    shaggystyle, Feb 18, 2004
    #1
    1. Advertising

  2. shaggystyle

    lallous Guest

    "shaggystyle" <> wrote in message
    news:...
    > I'm looking for a way to call system commands with variable parameters
    > within a Unix environment. Sort of like system() but i need to be
    > able to pass variable parameters....so I guess it would kind of look
    > like this:
    >
    > system("command -%s -%s", parameter1, parameter2);
    >
    > I've tried the above and it doesn't work when compiled with GCC.
    > Thanks.


    Hello

    Use sprintf()

    char temp[512];
    sprintf(temp, "command -%s -%s", parameter1, parameter2);
    system((char *)temp);

    However this was not a C++ question.
    --
    Elias
     
    lallous, Feb 18, 2004
    #2
    1. Advertising

  3. shaggystyle

    David Harmon Guest

    On Wed, 18 Feb 2004 18:05:32 +0200 in comp.lang.c++, "lallous"
    <> was alleged to have written:
    >Use sprintf()
    >
    >char temp[512];
    >sprintf(temp, "command -%s -%s", parameter1, parameter2);
    >system((char *)temp);
    >
    >However this was not a C++ question.


    std::eek:stringstream cmd;
    cmd << "command -" << parameter1 << " -" << parameter2;
    system(cmd.str().c_str());

    There, now it was a C++ question. :)
     
    David Harmon, Feb 18, 2004
    #3
  4. shaggystyle

    Clark Cox Guest

    In article <c102rb$1c777a$-berlin.de>,
    "lallous" <> wrote:

    > "shaggystyle" <> wrote in message
    > news:...
    > > I'm looking for a way to call system commands with variable parameters
    > > within a Unix environment. Sort of like system() but i need to be
    > > able to pass variable parameters....so I guess it would kind of look
    > > like this:
    > >
    > > system("command -%s -%s", parameter1, parameter2);
    > >
    > > I've tried the above and it doesn't work when compiled with GCC.
    > > Thanks.

    >
    > Hello
    >
    > Use sprintf()
    >
    > char temp[512];


    What if the command plus it's parameters is longer than 511 characters?
    Use ostringstream instead.

    > sprintf(temp, "command -%s -%s", parameter1, parameter2);
    > system((char *)temp);

    This cast is completely redundant.
     
    Clark Cox, Feb 18, 2004
    #4
  5. shaggystyle

    lallous Guest

    David Harmon <> wrote in message news:<>...
    > On Wed, 18 Feb 2004 18:05:32 +0200 in comp.lang.c++, "lallous"
    > <> was alleged to have written:
    > >Use sprintf()
    > >
    > >char temp[512];
    > >sprintf(temp, "command -%s -%s", parameter1, parameter2);
    > >system((char *)temp);
    > >
    > >However this was not a C++ question.

    >
    > std::eek:stringstream cmd;
    > cmd << "command -" << parameter1 << " -" << parameter2;
    > system(cmd.str().c_str());
    >
    > There, now it was a C++ question. :)


    Thanks for the tip!

    --
    Elias
     
    lallous, Feb 20, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    997
    Jonck van der Kogel
    May 27, 2004
  2. Felix Kater

    Variable number of parameters in a function call?

    Felix Kater, Oct 9, 2004, in forum: C Programming
    Replies:
    10
    Views:
    509
    John Bode
    Oct 11, 2004
  3. Replies:
    5
    Views:
    424
    Earl Purple
    Jan 5, 2006
  4. boriq
    Replies:
    4
    Views:
    598
    Tim Golden
    Jun 12, 2008
  5. prakash
    Replies:
    2
    Views:
    610
    prakash
    Apr 11, 2005
Loading...

Share This Page