how to call system command with variable parameters?

S

shaggystyle

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.
 
L

lallous

shaggystyle said:
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.
 
D

David Harmon

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. :)
 
C

Clark Cox

lallous said:
shaggystyle said:
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.
 
L

lallous

David Harmon said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top