akohan
asked on
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.
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.
are the parameters know at compile time?
ASKER
no, few of them will be passed by the user to main() function as argv[].
Thanks!
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[].
{
...
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[].
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys!
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.
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.
ASKER
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
>> 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);
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);