Results 1 to 6 of 6

Thread: Dos Commands

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Posts
    143
    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

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Posts
    143
    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);
    
    }

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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);
    
    }
    Harry.

    "From one thing, know ten thousand things."

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Posts
    143
    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

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    No problem
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width