Please forgive the fact that I'm not very familiar with C, but here goes nothing..
I'm using UNIX to write a small program in C, but I'm having some trouble with the system() function.
Is it possible to get system() to take an inputted string and insert it in a specified location of a BAsh command?
I've tried:
char host[50];
scanf("%s", %26amp;host);
system("ping -c 1 %s", host);
But that's not doing the trick. :) Any help would be very appreciated. %26gt;.%26lt; Thanks!
A question about C programming.?
Instead of approaching it that way, why not build the string beforehand:
char host[50];
char command[256];
scanf("%s", %26amp;host);
sprintf(command,"ping -c 1 %s", host);
system(command);
I think this gets you what you want, and is pretty flexible. Other people might point out that you could use strcat to append the host name to the command, but sprintf() will easily handle whatever command syntax you end up with.
pink
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment