Reputation: 960
I'm a C amateur, and I'm having trouble manipulating strings in C. The objective is to add the current pid
to a base string, then call it with system(system_call)
. I have the following:
char system_call[100] = "top -p "
char pid_string[30];
//quite a bit of other code
int main(int argc, char *argv[])
{
pid_t pid = getpid();
sprintf(pid_string,"%d",pid);
strcat(system_call,pid_string);
printf(system_call); //prints what I expect; something like 'top -p 5580'
system(system_call); //doesn't work
}
The system
call simply gives sh: system: not found
. I'm sure people good with C will know the problem instantly. I thought maybe the trailing 0 after strings in C had something to do with it, but I'm too terrible at C to recognize it or know what to do about it. I also tried system("%s",system_call)
but system
only takes one argument. Is there something wrong with my memory allocation? Any insight is appreciated.
Upvotes: 0
Views: 276
Reputation: 1774
Cant see any problem with your string construction, maybe the problem is that "system" itself does not work for some reason on your system :-), or "top" is not acessible
Upvotes: 2