parse cmd-line

hi all,
I'm need with your help. Suggest me please not hard way to parse like with command-line...
192.168.10.0/24 192.168.20.0/24 192.168.30.0/24 192.168.40.0/24
Just me need took ip address from either network segment in ordering subnet prefix.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h> // include standard input / output library
int main(int argc, char* argv[]) // main function - argc = number of arguments, argv = array holding argc amount of arguments
{
  if(argc > 1) // if there is more than one argument (ie more than file name given)
  {
	unsigned char bytes[4]; // create bytes to hold the part of the IP address
	unsigned char other; // hold the data after the /
	for(int c = 1; c != argc; c++) // a loop to go through all of the arguments passed
	{
	  printf("Argument %d: %s\n", c, argv[c]); // print out the argument & argument number
	  sscanf(argv[c], "%d.%d.%d.%d.%d/%d", &bytes[0], &bytes[1], &bytes[2], &bytes[3], &other); // scan the string to get separate parts of it
	  printf("\t%d\n\t%d\n\t%d\n\t%d\n\t/%d\n", bytes[0], bytes[1], bytes[2], bytes[3], other); // print them out, with some formatting
	} // end the for loop
  } // end the argc if-statement
  else // if there was no extra parameters passed to the program
  {
    puts("No command line given"); // then give some output in the console
  }
  getchar(); // wait for user input so that the user can read the console output
  return 0; // return without error
} // end the main loop 
Boost has a Command-Line parser:
http://www.boost.org/doc/libs/1_35_0/doc/html/program_options.html

Or you can google for another one called GetPot. Both of these will provide you with some nicely encapsulated methods to read the command line easily.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main( int argc, char * args[] )
{
    // an example that just does the first argument
    string arg( args[1] );
    size_t slashpos( arg.find( '/' ) );
    // slashpos should be validated here
    string ip( arg.substr( 0, slashpos ) );
    string othernum( arg.substr( slashpos + 1 ) );

    cout << ip << endl;
    cout << other << endl;

    return 0;
}
no, me need took either ip-address from subnet. I can parse argv and calculi quantity of hosts, bit I'm confused pertaining find out first and last address from chose subnet. for example...
1
2
3
4
5
6
7
8
9
10
11
unsigned int ip[4];
unsigned int mask;
int degree;
int hosts;
int count;
sscanf(argv[i], "%d.%d.%d.%d/%d", &ip[0], &ip[1], &ip[2], &ip[3], &mask);
degree = 32 - mask;
hosts=pow(2,degree);
for(hosts>count;count++){

}

I can set 192.168.10.19/28 or 192.168.10.24/28, but all ip address compared with this subnet 192.168.10.16/28
Which's way I might set first 192.168.10.17, and last 192.168.10.31?
So, I've quantity hosts and like that chars...
&ip[0]= 192
&ip[1]= 168
&ip[2]= 10
&ip[3]= 24
In what way I'll get a first adders from that subnet? It I've got &ip[3]= 16 it will be most easy, me necessary anticipated that. How to do it?
Topic archived. No new replies allowed.