hello everyone
i want to input ip like a char,then split into "127 0 0 1"
so that i can scan ips for start ip to end ip.
I tried with strtok spliting but program crashes when i do
sprintf (buffer, "%s",pch[1]);
hello everyone
i want to input ip like a char,then split into "127 0 0 1"
so that i can scan ips for start ip to end ip.
I tried with strtok spliting but program crashes when i do
sprintf (buffer, "%s",pch[1]);
Could you please post the code you have. The information you've provided so far isn't enough to do anything more than take wild guesses.
#include <stdio.h>
#include <iostream.h>
#include <string.h>
int main ()
{
char buffer [50];
char buf[50];
char * pch;
cin >> buf;
pch = strtok (buf,".");
for (pch[3] = 1;pch[3] < 255;pch[3]++)
{
sprintf (buffer, "%s.%s.%s.%s",pch[0],pch[1],pch[2],pch[3]);
}
return 0;
}
I think this is an excellent use case for strtol
. It allows you to provide an end marker that indicates where the last translation ended. For a well-formed (see note below) IP string, that is always going to be a period. Given that, you can construct something like:
#include <cstdio>
#include <cstdlib>
int main (int argc, char ** argv) {
char * ip = argv[1], *next = 0;
long quad[3] = {0};
for (int i = 0; i < 3; ++i) {
quad[i] = strtol (ip, &next, 10);
ip = next + 1;
}
for (int i = 1; i < 255; ++i)
printf ("%d.%d.%d.%d\n", quad[0], quad[1], quad[2], i);
return 0;
}
And it will parse the first three octets and generate a sequence of all addresses with a /24 mask. For example:
$ ./a.out 192.168.10.0
192.168.10.1
192.168.10.2
192.168.10.3
192.168.10.4
192.168.10.5
192.168.10.6
...
192.168.10.249
192.168.10.250
192.168.10.251
192.168.10.252
192.168.10.253
192.168.10.254
NOTE: It is up to you to verify well-formedness. My example elides much error checking for the sake of brevity.
yes i see that you are on linux.im on the windows and its the same program chrashes
I don't have access to a Windows build environment at the moment. Once I get to one I will try to reproduce the problem you are seeing.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.