• 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?
 
sunnycoderConnect With a Mentor Commented:
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
 
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
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
 
ozoConnect With a Mentor Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.