I am coming from java, and I cannot figure this out. I am trying to concatenate strings to run a command with parameters using the system function, this is what I am doing, but it is not working:
system("command "+param1+" other stuff "+param3);
I am coming from java, and I cannot figure this out. I am trying to concatenate strings to run a command with parameters using the system function, this is what I am doing, but it is not working:
system("command "+param1+" other stuff "+param3);
To concatenate string in C use snprintf
In C strings are representing as a array of chars. Their name is pointer to their first element. Every operation should be done using functions. In other case you simply make pointer arithmetic. So it your example you try to launch function from address that probably doesn't even exist in system.
Do not use strcat
! It is dangerous If you really need simple function to call use strlcat
For more information you need back to basics
Probably you want to build your command string with a function like snprintf
: you have the ability to insert in your string various types (not only strings) and you are safeguarded against buffer overflows (check its return value!).
char buffer[256];
if(snprintf(buffer, sizeof(buffer), "command %s other stuff %s", param1, param3)>=sizeof(buffer))
{
/* the buffer isn't big enough */
}
else
system(buffer);
There is no operator overloading in c
. You have to create enough buffer and then use library function strcat
.