468,161 Members | 2,056 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

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....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.
Jul 22 '05 #1
4 9475
"shaggystyle" <sh*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
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
Jul 22 '05 #2
On Wed, 18 Feb 2004 18:05:32 +0200 in comp.lang.c++, "lallous"
<la*****@lgwm.org> 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::ostringstream cmd;
cmd << "command -" << parameter1 << " -" << parameter2;
system(cmd.str().c_str());

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

Jul 22 '05 #3
In article <c1*************@ID-161723.news.uni-berlin.de>,
"lallous" <la*****@lgwm.org> wrote:
"shaggystyle" <sh*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
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.

Jul 22 '05 #4
David Harmon <so****@netcom.com> wrote in message news:<40****************@news.west.earthlink.net>. ..
On Wed, 18 Feb 2004 18:05:32 +0200 in comp.lang.c++, "lallous"
<la*****@lgwm.org> 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::ostringstream cmd;
cmd << "command -" << parameter1 << " -" << parameter2;
system(cmd.str().c_str());

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


Thanks for the tip!

--
Elias
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Brian Conway | last post: by
6 posts views Thread by JLK | last post: by
3 posts views Thread by Chris | last post: by
2 posts views Thread by Steven K | last post: by
9 posts views Thread by C++ Newbie | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.