-
Hello,
I am pretty new to C programming and I would like to know how to do the following.
User types in Ip addy and Then pings ip. Here is my code now. I know all basic commands, but the system() isnt allowing two parameters. So thats where im lost.
Code:
#include <stdlib.h>
#include <stdio.h>
void main(void) {
float ip;
printf("Enter in ip addy: ");
scanf("%f",&ip);
system("ping %f", ip);
}
Any suggestions ?
Thanks
-
Are you sure an IP address can be expressed as a floating point number? I would have thought you would need a string for that.
-
Okay how about this.
But it still errors on the two parameters.
Code:
#include <stdlib.h>
#include <stdio.h>
void main(void) {
char ip[50];
printf("Enter in ip addy: ");
fgets(ip,sizeof(ip),stdin);
sscanf(ip, "%s", ip);
system("ping %f", ip);
}
-
I think you have the wrong idea of format strings - you can't use them in every function, only some. Use sprintf() to create the string first and then send it to system(), like this:
Code:
#include <stdlib.h>
#include <stdio.h>
void main(void)
{
char ip[50];
printf("Enter in ip addy: ");
fgets(ip,sizeof(ip),stdin);
sscanf(ip, "%s", ip);
sprintf(ip, "ping %s", ip);
system(ip);
}
-
See I learn something new every day.
I never knew about sprintf().
:)
Although it does not error , when it runs it just returns "unknown host command".
But at least now im on the right track :)
Thanks Harry
-