how to call system command with variable parameters?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shaggystyle

    how to call system command with variable parameters?

    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....s o 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.
  • lallous

    #2
    Re: how to call system command with variable parameters?

    "shaggystyl e" <shaggystyle@ho tmail.com> wrote in message
    news:6d0c4c3b.0 402180805.1ae30 ec4@posting.goo gle.com...[color=blue]
    > 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....s o 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.[/color]

    Hello

    Use sprintf()

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

    However this was not a C++ question.
    --
    Elias


    Comment

    • David Harmon

      #3
      Re: how to call system command with variable parameters?

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

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

      There, now it was a C++ question. :-)

      Comment

      • Clark Cox

        #4
        Re: how to call system command with variable parameters?

        In article <c102rb$1c777a$ 1@ID-161723.news.uni-berlin.de>,
        "lallous" <lallous@lgwm.o rg> wrote:
        [color=blue]
        > "shaggystyl e" <shaggystyle@ho tmail.com> wrote in message
        > news:6d0c4c3b.0 402180805.1ae30 ec4@posting.goo gle.com...[color=green]
        > > 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....s o 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.[/color]
        >
        > Hello
        >
        > Use sprintf()
        >
        > char temp[512];[/color]

        What if the command plus it's parameters is longer than 511 characters?
        Use ostringstream instead.
        [color=blue]
        > sprintf(temp, "command -%s -%s", parameter1, parameter2);
        > system((char *)temp);[/color]
        This cast is completely redundant.

        Comment

        • lallous

          #5
          Re: how to call system command with variable parameters?

          David Harmon <source@netcom. com> wrote in message news:<409991bf. 239285043@news. west.earthlink. net>...[color=blue]
          > On Wed, 18 Feb 2004 18:05:32 +0200 in comp.lang.c++, "lallous"
          > <lallous@lgwm.o rg> was alleged to have written:[color=green]
          > >Use sprintf()
          > >
          > >char temp[512];
          > >sprintf(temp , "command -%s -%s", parameter1, parameter2);
          > >system((char *)temp);
          > >
          > >However this was not a C++ question.[/color]
          >
          > std::ostringstr eam cmd;
          > cmd << "command -" << parameter1 << " -" << parameter2;
          > system(cmd.str( ).c_str());
          >
          > There, now it was a C++ question. :-)[/color]

          Thanks for the tip!

          --
          Elias

          Comment

          Working...