Thread: need help with this small code... pls..

  1. #1

    need help with this small code... pls..

    Hi there, first of all, i want to make the "ip1" char, go 1 higher, and i don't know how to do that. (like from to

    And second, i'd like to know how to use it in system(); (or something else that does the same). i've tried adding it to system, but i get this error,
    error C2660: 'system' : function does not take 3 parameters

    How can i do this, please help.... i've searched my ass off..... hehe

    int main()
    	char ip1[15];
    	char ip2[15];
    	cout << "Start IP: ";
    	 cin >> ip1;
    	cout << "Stop IP: ";
    	 cin >> ip2;
    	if (ip1<ip2)
    		cout << "Start IP can not be higher than Stop IP." << endl;
    		cout << "Scanning: [" << ip1 << "] -> [" << ip2 << "]" << endl;
    			/* Here i want to add the "ip1" char to system(); */
    			/* Here i want the "ip1" char to go 1 higher. */
    	return 0;

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    if (ip1<ip2)
    That isn't doing what you want it to do. It's not comparing them lexigraphically (IE, 'a' < 'b' < 'c'), but rather simply comparing the addresses.

    It would be easiest to store the ips as 4 chars (think of them as bytes rather than letters, one for each octet in the IP), and then convert them to the human readable ip format

    #include <iostream>
    #include <cstdlib>
    #include <sstream>
    int main() {
    	unsigned char ip[4];
    	unsigned char upperLim;
    	int tempInt;  // read stuff as integers rather than characters
    	std::cout << "Enter space delimited IP, ie, 192 168 1 1\n";
    	for (int i = 0; i < 4; ++i) {
    		std::cin >> tempInt;
    		ip[i] = (unsigned char) tempInt;
    	std::cout << "Enter upper limit (inclusive, last octet only): ";
    	std::cin >> tempInt;
    	upperLim = (unsigned char) tempInt;
    	do {
    		std::ostringstream out; // use ostringstream to write the bytes into a string
    		for (int i = 0; i < 3; ++i) { // write out first 3 with a ,
    			out << (int) ip[i] << '.';
    		out << (int) ip[3];
    		std::cout << out.str() << '\n';
    // unixesque ping args, dunno if windows supports the -c option to ping or output redirection
    		std::string command_line_arg = "ping -c 1 " + out.str(); // + ">"  +out.str() + ".result";
    	} while  (ip[3] < upperLim);
    	return 0;
    I sense some malicous use of this code... don't try to destroy stuff with it please.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    I lurk
    Join Date
    Aug 2002
    Before calling system() you'll need to combine the strings/integers/other stuff using sprintf();


    char ip1[] = "";
    char pszBuffer[32];

    sprintf(pszBuffer, "ping %s", ip1);
    // pszBuffer now contains "ping;

    Now, to increment the IP is going to be a little tricky. You could make your own structure / class for IP's which would encapsulate all this... but here goes.

    char *incrementIP(char *pszIP)
    	char cDelim = '.';
    	unsigned int iIPField[4];
    	int i = 0;
    	do {
    		iIPField[i] = atoi(strtok(NULL, cDelim));
    	} while (i < 4);
    	i = 4;
    	while (iIPField[i] == 255)
    		iIPField[i] = 0;
    	} else {
    	char pszReturnIP[16];
    	sprintf(pszReturnIP, "%s.%s.%s.%s", iIPField[0], iIPField[1], iIPField[2], iIPField[3]);
    	return pszReturnIP;
    Note, I didn't test this... it's almost 3AM, I have no clue why I just wrote that for you but I did... the function doesn't check for validity of the IP address... it assumes a real address is given (4 fields seperated by periods.

  4. #4
    wow, thx guys....... don't worry i'm not gonna 'destroy' anything.

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Originally posted by SuperGirl
    wow, thx guys....... don't worry i'm not gonna 'destroy' anything.
    i'm listening; what non-malicious use do you have for this code?
    hello, internet!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Another approach:
    #include <iostream>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    using namespace std;
    int main(void)
        struct in_addr addr;
        unsigned long host_order;
        addr.s_addr = inet_addr("");
        host_order = ntohl(addr.s_addr);
        for (int i = 0; i < 10; i++)
            cout <<inet_ntoa(addr) <<endl;
            addr.s_addr = htonl(++host_order);
    	return 0;
    The IP address is stored as a long, as returned from inet_addr(), but it's in network byte order. So, using ntohl(), convert it to host order in order to store a copy. Then increment the copy and update the network ordered version when you want to use it.

    This code ain't bullet proof, and requires Unix style sockets. But it can be adapted for Winsock use if that's what you want.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small code works on VS6 crash on VS2005 WHY???
    By salvadoravi in forum C Programming
    Replies: 7
    Last Post: 02-10-2008, 09:32 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. help on my code PLS!!!
    By gtr_s15 in forum C++ Programming
    Replies: 3
    Last Post: 12-21-2005, 07:48 AM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. code fragment pls help
    By Damo in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 09:57 AM