How do i write this kind of program - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How do i write this kind of program

wyz0020wyz0020 Posts: 120Member
sorry that i didn't state clearly what i am trying to ask, because it's hard to cover my question in just one sentence.
How can i make a program that does MS-DOS commands, like i wanna write a program in C that ping an ip, or copy a file to somewhere else. People can, of course, open MS-DOS MODE and type in "ping 127.0.0.1", but i wanna make a program which does that automaticly so that the user can just double click the program, and the program will ping the machine itself.
Can that be done?

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : sorry that i didn't state clearly what i am trying to ask, because it's hard to cover my question in just one sentence.
    : How can i make a program that does MS-DOS commands, like i wanna write a program in C that ping an ip, or copy a file to somewhere else. People can, of course, open MS-DOS MODE and type in "ping 127.0.0.1", but i wanna make a program which does that automaticly so that the user can just double click the program, and the program will ping the machine itself.
    : Can that be done?
    :
    [blue]Well, yes, that can be done. But I don't see the point in pinging the same computer on wich the program is run! [/blue]
    [code]
    #include

    int main(int argc, char *argv[])
    {
    char command[126];
    char *ip;
    if(argc == 2)
    ip = argv[1];
    else
    ip = "127.0.0.1";
    sprintf(command,"ping %s", ip);
    system(command);
    return 0;
    }
    [/code]

  • tom_swtom_sw Posts: 828Member
    [b][red]This message was edited by tom_sw at 2003-7-3 8:47:28[/red][/b][hr]
    : : sorry that i didn't state clearly what i am trying to ask, because it's hard to cover my question in just one sentence.
    : : How can i make a program that does MS-DOS commands, like i wanna write a program in C that ping an ip, or copy a file to somewhere else. People can, of course, open MS-DOS MODE and type in "ping 127.0.0.1", but i wanna make a program which does that automaticly so that the user can just double click the program, and the program will ping the machine itself.
    : : Can that be done?
    : :
    : [blue]Well, yes, that can be done. But I don't see the point in pinging the same computer on wich the program is run! [/blue]
    : [code]
    : #include
    :
    : int main(int argc, char *argv[])
    : {
    : char command[126];
    : char *ip;
    : if(argc == 2)
    : ip = argv[1];
    : else
    : ip = "127.0.0.1";
    : sprintf(command,"ping %s", ip);
    : system(command);
    : return 0;
    : }
    : [/code]
    :
    :
    Using the KISS principle, you could just use a batch file (pingme.bat for example) containing the line 'ping 127.0.0.1'

    Then make an shortcut to that file. When you click it, it will open a Command prompt box, and run the command. If you find it is closing the box at the end of the program and you want it to stay open, and a line that says 'pause'.

    It's not nearly as 'cool' as using C++, but it will work just fine and you'll have it ready in 2 minutes.



  • wyz0020wyz0020 Posts: 120Member
    Thank you very much. I wasn't gonna ping 127.0.0.1. it's just an example. i think it'll be more clear then just make up an ip.
    but thanks anyway.
  • wyz0020wyz0020 Posts: 120Member
    hi, i have two more question:
    in the following code,

    int main(int argc, char *argv[])
    {
    char command[126];
    char *ip;
    if(argc == 2)
    ip = argv[1];
    else
    ip = "127.0.0.1";
    sprintf(command,"ping %s", ip);
    system(command);
    return 0;
    }

    what does the fourth line( if(argc == 2)) and the fifth line (ip = argv[1]) do?

    the second question is when i execute and run the program, it show up a dialog box which ask me to enter the parameter? what should i do with that?
    Thank you! :)

  • tom_swtom_sw Posts: 828Member
    : hi, i have two more question:
    : in the following code,
    : [code]
    : int main(int argc, char *argv[])
    : {
    : char command[126];
    : char *ip;
    : if(argc == 2) [red]// This checks for incoming command line arguments[/red]
    : ip = argv[1]; [red]// if there is one, it gets the value for your ping[/red]
    : else
    : ip = "127.0.0.1"; [red] // otherwise uses the default value[/red]
    : sprintf(command,"ping %s", ip);
    : system(command);
    : return 0;
    : }
    : [/code]
    : what does the fourth line( if(argc == 2)) and the fifth line (ip = argv[1]) do?
    :
    : the second question is when i execute and run the program, it show up a dialog box which ask me to enter the parameter? what should i do with that?
    : Thank you! :)
    :
    :

  • wyz0020wyz0020 Posts: 120Member
    I learned a lot today. so why the (if(argc == 2)) is == 2 not ==1 or ==0? and i didn't see any scanf or getchar commands. Where does the user suppose to enter data, in our case, the ip. Is it once you use the main(int argc, char *argv[]) thing, it'll pop up a dialog box for you to input data? or is the system(command) did the trick? I found some articles about the main(int argc....) thing, but i couldn't understand. can you teach me how it works? it'll be better if someone teaches me. Thank you.
  • stoberstober Posts: 9,765Member ✭✭✭
    : I learned a lot today. so why the (if(argc == 2)) is == 2 not ==1 or ==0? and i didn't see any scanf or getchar commands. Where does the user suppose to enter data, in our case, the ip. Is it once you use the main(int argc, char *argv[]) thing, it'll pop up a dialog box for you to input data? or is the system(command) did the trick? I found some articles about the main(int argc....) thing, but i couldn't understand. can you teach me how it works? it'll be better if someone teaches me. Thank you.
    :

    [blue]That little program assumes you will enter the value on the DOS command-line, for example:[/blue]
    [code]
    c:>MyPng 127.0.0.1
    [/code]
    [blue]Now then MyPng.exe is executed, MS-DOS will pass two parameters to it: Parameter #1 is always the name of the program, and Parameter #2 is the ip address 127.0.0.1. That is why argc == 2. argv[0] = the program name and argv[1] = the ip address.

    If you did not enter the IP address, then argc == 1 and argv[0] == the name of the program.[/blue]
  • wyz0020wyz0020 Posts: 120Member
    thank you, i appreciate your help.
    but the program didn't seems to work. i name the program ping1.exe. when i run it under MS-DOS, i entered c:>ping1 127.0.0.1 [ENTER], then it kind of run for a second, then it stoped, no report, nothing. it went back to c:> . kind of like this:
    c:>
    c:>ping1 127.0.0.1 [ENTER] //run for a second
    // went back to
    c:>

    the whole thing is like that, nothing appeared. if i use ping command in dos, it'll tell me how much byte did i send, and did the target machine recieved the packages or not.
    but my ping1 seems like does nothing. did it really ping the target machine already?
  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : the whole thing is like that, nothing appeared. if i use ping command in dos, it'll tell me how much byte did i send, and did the target machine recieved the packages or not.
    : but my ping1 seems like does nothing. did it really ping the target machine already?
    :

    I am using XP operating system and get this output

    [code]

    Pinging 127.0.0.1 with 32 bytes of data:

    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

    Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
    Press any key to continue
    [/code]

Sign In or Register to comment.