[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

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.

0
akohan
Asked:
akohan
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now