• C

How to customize part of a string?

Hello group,

I need to call an external command in my C code. Let's say I need to call ls command but passing parameters could be variable. How can I do that?
I was planning to do it using sprtinf() but I rather go with char* string type.

so instead of  using array and sprintf(buf, "ls %s -t ", filename);
I want to do it using char* i.e.

    char* cmdstring = "/home/user1/cmd1 %s %d"  // I know this is not valid but how can I do it using  char*  ?

but I'm not sure how I can change those parameters in char* as we can do in sprintf.

any idea is appreciated.

Regards.

akohanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
are the parameters know at compile time?
0
akohanAuthor Commented:

no, few of them will be passed by the user to main() function as argv[].

Thanks!

0
sunnycoderCommented:
int main (int argc, char ** argv)
{
   ...
   char * buf [MAX_BUF];
   ..
   sprintf(buf,"%s %s %s %d", command, argv[1], argv[2], 100);
   ...
}

You can use %s to add a string literal or a char[].
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ozoCommented:
char * buf [MAX_BUF];
should be
 char  buf [MAX_BUF];
0
akohanAuthor Commented:

Hi Sunnycoder,

Thanks for the response, currently I'm doing the same but I was wondering if there is any way of using it as string (char*) rather array (buf).

sorry, I guess I didn't explain my question very well.

Regards.
0
sunnycoderCommented:
char * and char[] are interchangable

char * = "hello"; may store hello in a read only segment because here you are not specifying the amount of storage to use.

You can allocate memory dynamically

char * buf = malloc(MAX_BUF);

sprintf (buf, ... );

the only difference in this and previous code is that here memory is on heap and previously it was on stack.
0
sunnycoderCommented:
good catch ozo. :)

akohan

int main (int argc, char ** argv)
{
   ...
   char  buf [MAX_BUF];
   char * command = buf;
   ..
   sprintf(command,"%s %s %s %d", command, argv[1], argv[2], 100);
   ...
}

this would work the same.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akohanAuthor Commented:

Thanks guys!


0
Infinity08Commented:
If the amount of parameters will only be known at run time, you can take a look at vsprintf :

        http://www.cplusplus.com/reference/clibrary/cstdio/vsprintf.html


btw :

>>     char* cmdstring = "/home/user1/cmd1 %s %d"  // I know this is not valid but how can I do it using  char*  ?

is perfectly valid.
0
akohanAuthor Commented:


Thank you Infinity!!! I used to think that format only would apply for sprintf() and vsprintf(). Thanks for the hint and also pointing out to vsprintf().

Regards,
ak
0
Infinity08Commented:
>> I used to think that format only would apply for sprintf() and vsprintf().

The cmdstring format string created above can be passed as second parameter to sprintf, like this :

        char* cmdstring = "/home/user1/cmd1 %s %d";
        sprintf(str, cmdstring, arg1, arg2);

and it will do the same as just :

        sprintf(str, "/home/user1/cmd1 %s %d", arg1, arg2);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.